Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / printing

How to Silently Print PDFs using Adobe Reader and C#

4.81/5 (9 votes)
23 May 2016CPOL2 min read 36.2K  
This is an alternative for How to Silently Print PDFs using Adobe Reader and C#

Introduction

If you want to print a PDF document to another printer than the default printer, you need to use some other switches than in the original article.

Background

As I have more than one printer in my network and Windows 10 likes to change the default printer every time I change the properties of a printer, I needed to make sure that the PDF document was printed with one specific printer.

I got tired of PDF documents printed on photo paper.

Using the Code

This is only a code snippet and assumes that there is a variable containing the path to the PDF file.

C#
string pathPdf = "...";  // The path to your document

And this is the full code:

C#
ProcessStartInfo infoPrintPdf = new ProcessStartInfo();
infoPrintPdf.FileName = pathPdf;
// The printer name is hardcoded here, but normally I get this from a combobox with all printers
string printerName = "Samsung M262x 282x Series";
string driverName = "printqueue.inf";
string portName = "SEC30CDA792D770";
infoPrintPdf.FileName = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
infoPrintPdf.Arguments = string.Format("/t {0} \"{1}\" \"{2}\" \"{3}\"", 
    pathPdf, printerName, driverName, portName);
infoPrintPdf.CreateNoWindow = true;
infoPrintPdf.UseShellExecute = false;
infoPrintPdf.WindowStyle = ProcessWindowStyle.Hidden;
Process printPdf = new Process();
printPdf.StartInfo = infoPrintPdf;
printPdf.Start();

// This time depends on the printer, but has to be long enough to
// let the printer start printing
System.Threading.Thread.Sleep(10000);  

if (!printPdf.CloseMainWindow())              // CloseMainWindow never seems to succeed
    printPdf.Kill(); printPdf.WaitForExit();  // Kill AcroRd32.exe

printPdf.Close();  // Close the process and release resources

How to Find the Printer Variables

In the document Acrobat FAQ on page 27, this information can be found:

AcroRd32.exe /t path "printername" "drivername" "portname"

— Initiates Adobe Reader and prints a file, whose path must be fully specified, while suppressing the Print dialog box. The four parameters of the /t option evaluate to path, printername, drivername, and portname (all strings).

  • printername — The name of your printer.
  • drivername — Your printer driver’s name, as it appears in your printer’s properties.
  • portname — The printer’s port. portname cannot contain any "/" characters; if it does, output is routed to the default port for that printer.

The steps below might be a bit simplistic for some, but maybe not for everyone.

Note: I am using Windows 10, the exact steps might differ on other versions of Windows.

1. Open the Control Panel and select View devices and printers

Right-click on the wanted printer to show the context menu:

Printer Context Menu

1.1. To start with, select Printer Properties

Printer Properties Dialog

On the General tab, you can find the printer name.

1.2. Select the Ports tab and then click on Configure Port

Port Tab

Port Name

Now you have the port name.

1.3. You can now close the dialogs.

2. Driver Name

The last information to be found is the driver name. On Windows 10, I found it to be a bit tricky and it is very possible there is a much easier way. Right-click on the printer again, but this time select Properties at the very bottom.

2.1 In the dialog that opens, select the tab Hardware

Properties - Hardware

2.2 Click the button Properties to show the next dialog and select the tab Details
2.3 Select the property Inf Name in the combo box, and voilà; there is the last piece of information.

Properties - Details

The value is the printer driver.

History

  • First release of the tip

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)