Monday, May 27, 2013

How to Silently Print Pdfs using Adobe Reader and C#

This tip is merely to show a way in which you can launch adobe and send a PDF straight to the printer in one fail swoop without using a 3rd party solution. (PdfSharp/iTextSharp...etc...).

After reviewing some of adobe's documentation  on command line switches, i was able to throw together a bit of code that would send a Pdf straight to printer without any user interaction. Attempting to send post script straight to the printer can get nasty/complicated so this is meant to be an easy solution on how to take a newly created Pdf/existing Pdf from your app and send it directly to the printer.

The Code Explained
The code below is being used in a console application. I know it would be easily incorporated into any Gui based desktop application as well.


     1) Print all pdf's from a defined directory

string[] files = Directory.GetFiles(@"c:\temp");
foreach (string file in files.Where(file => file.ToUpper().Contains(".pdf")))
      2) Simply print one Pdf File 



I am going to make an assumption that the basics of using the Process class is understood. 

//Define location of adobe reader/command line switches to launch adobe in "print" mode
proc.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;   

 1) proc.StartInfo.FileName

The value should be the absoulte path to your adobe reader instance (should work with acrobat as well). As far as i can tell in my research, most of the current versions of adobe reader should support the command line switches

 2)  proc.StartInfo.Arguments

These are your command line switches to be applied to adobe reader.

/p <filename>    => means open and go straight to print dialog

/h    => open adobe reader as a minimized window

3) KillAdobe("AcroRd32");

Read more: Codeproject
QR: Inline image 1