0

Here's a simple question from a C# newbie.

My goal is to print a file directly from C# code, ideally without showing the print dialog and choosing some options. Is there any Windows API I can rely on and call? For instance, something which would let me choose how many copies to print, which printer and send it directly to it.

Any suggestion? Thanks!

glw7v8
  • 83
  • 1
  • 2
  • 8
  • There's no accepted answer, but maybe this?: https://stackoverflow.com/questions/17590896/auto-print-without-dialog – David Jan 24 '18 at 12:47

2 Answers2

1

According to this post Print Pdf in C#

you only have to start a new process:

Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
    CreateNoWindow = true,
    Verb = "print",
    FileName = path //put the correct path here
};
p.Start();


It works fine, when there is a pdf reader like Adobe Acrobat Reader installed!

Mark
  • 307
  • 2
  • 12
1

"Print a file" is rather vague.

If you can find a .NET library (or write your own) to open the specific type of file, read the content, then render the content, you can use the in-built .NET classes such as the FixedDocument class (referenced in Auto print without dialog ) to build up your print output and send it to a printer.

If you want to print an arbitrary file on the filesystem, and are assuming that there is a program installed that can open and print that type of file, and that the program installed a "Print" verb into the right-click menu, then you should be able to use the "ProcessStartInfo" method from Mark's answer above. That simulates the user right-clicking on the file and selecting an option named "Print".

Either way, printing on Windows with no user input is a separate problem in itself. The print dialogue that appears is often part of the driver for that specific printer (rather than the generic Windows printer dialogue) and includes options that are specific to that printer, such as duplexing, or selecting a specific paper tray, etc. Many of these drivers provide no programmatic method of setting these options at all, and those that do often need code that is specific to that driver. You can programmatically specify the generic options (such as number of copies), but any extra features will either be disabled or will use default values.

Hope this helps

Trevor
  • 1,251
  • 1
  • 9
  • 11
  • Thanks Trevor for the detailed explanation. Yes, my goal is to print an arbitrary file on the file system which can be open and print; therefore, I'll try following the direction listed below by Mark! – glw7v8 Jan 25 '18 at 11:34