13

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 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'.

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
Immanu'el Smith
  • 683
  • 2
  • 8
  • 18
  • possible duplicate of [How to Print any document in a SELECTED printer](http://stackoverflow.com/questions/3138181/how-to-print-any-document-in-a-selected-printer) – Rowland Shaw Mar 03 '11 at 16:43

3 Answers3

29

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);
    }
}
dataCore
  • 1,509
  • 1
  • 16
  • 17
  • 2
    Works for me with pdf files too! Thanks - helped me alot – pila Nov 24 '12 at 15:37
  • Can we pass whole print settings from print dialog? – huMpty duMpty Mar 04 '14 at 09:22
  • @huMpty duMpty: What do you mean by "pass whole print settings"? You allready receive all print settings from the print dialog by using "yourPrintDialog1.PrinterSettings." – dataCore Mar 06 '14 at 15:10
  • Its work for me, but what if printer is not available or not connected. Is their any way we can show a error message to user that printer is not available in such cases. – Avneesh Srivastava Jul 28 '14 at 08:54
  • @AvneeshSrivastava: didn't testet if the printer has to be available. Maybe the windows printDialog will taking care of it... – dataCore Sep 23 '14 at 15:20
  • @dataCore if I print like this with a process and a PrintDialog the Printer ignores the settings that I selected in the Dialog, like paper source or orientation. any Idea? – Mong Zhu May 31 '16 at 12:59
  • @dataCore no I haven't but if I print a PDF from a Reader the Dialog settings work and it takes the selected paper source/tray. I found a post (can't find it again) where an answer said, that for this to work I would have to use `PrintDocument` and set the entire layout myself. That is too much overhead. – Mong Zhu Jun 03 '16 at 14:31
  • Why this "\"" before and at the end of the printername ? – bigtheo Jun 13 '21 at 18:29
5

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";
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

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.

Tom
  • 11
  • 2