开发者

Is there anyway to specify a PrintTo printer when spawning a process?

开发者 https://www.devze.com 2023-01-05 21:19 出处:网络
What I Have I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified address

What I Have

I am currently writing a program which takes a specified file and the performs some action with it. Currently it opens it, and/or attaches it to an email and mails it to specified addresses.

The file can either be of the formats: Excel, Excel Report, Word, or PDF.

What I am currently doing is spawning a process with the path of the file and then starting the process; however I开发者_如何学JAVA also am trying to fix a bug feature that I added which adds the verb 'PrintTo' to the startup information, depending on a specified setting.

What I Need

The task I am trying to accomplish is that I would like to have the document open and then print itself to a specified printer named within the program itself. Following that up, the file should then close itself automatically.

If there is no way to do this generically, we might be able to come up with a way to do it for each separate file type.

What you Need

Here is the code I'm using:

ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = FilePath;

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
       // TODO: Add default printer.
    }

    pStartInfo.Verb = "PrintTo";
}

// Open the report file unless only set to be emailed.
if ((!Email && !Print) || Print)
{
    Process p = Process.Start(pStartInfo);
}

How I'm doing...

Still stumped... might call it like Microsoft does,'That was by design'.


The following works for me (tested with *.doc and *.docx files)

the windows printto dialog appears by using the "System.Windows.Forms.PrintDialog" and for the "System.Diagnostics.ProcessStartInfo" I just take the selected printer :)

just replace the FILENAME with the FullName (Path+Name) of your Office file. I think this will also work with other files...

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(**FILENAME**);
        info.Arguments = "\"" + printDialog1.PrinterSettings.PrinterName + "\"";
        info.CreateNoWindow = true;
        info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        info.UseShellExecute = true;
        info.Verb = "PrintTo";
        System.Diagnostics.Process.Start(info);
    }
}


Theoretically, according to an article on MSDN you should be able to change it to be along the lines of (untested):

// Determine wether to just open or print
if (Print)
{
    if (PrinterName != null)
    {
        pStartInfo.Arguments = "\"" + PrinterName + "\"";
    }

    pStartInfo.CreateNoWindow = true;
    pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pStartInfo.UseShellExecute = true;
    pStartInfo.WorkingDirectory = sDocPath;

    pStartInfo.Verb = "PrintTo";
}


get from Rowland Shaw:

               ProcessStartInfo startInfo = new ProcessStartInfo(Url)
                {
                    Verb = "PrintTo",
                    FileName = FilePath,
                    CreateNoWindow = true,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = true,
                    Arguments = "\"" + PrinterName+ "\"",
                };
                Process.Start(startInfo);

FilePath look like 'D:\EECSystem\AttachedFilesUS\53976793.pdf'

PrinterName is your printer name

copy the code,it will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号