Print a PDF in c#

Adobe does not make it easy to print a PDF from code unless you buy expense libraries. So the easiest way is to have AcroRd32.exe (Reader) print it by starting external process and passing commandline args.  Not the cleanest solution, but it works.  You can also queue multiple documents by changing the Arguments and calling Start() again.  This queues another request that the reader will pickup after printing current document.

public static void PrintPDF(string path, string printerName)
{
    //Args - http://support.adobe.com/devsup/devsup.nsf/docs/52080.htm

    // This does leave the Acrobat reader open. No easy way to close it after print.
    string args = string.Format("/t \"{0}\" \"{1}\"", path, printerName);

    Process process = new Process();
    process.StartInfo.FileName = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";
    process.StartInfo.Arguments = args;
    process.StartInfo.UseShellExecute = false;
    process.Start();
}

Print multiple PDF’s:

public static void PrintPDF(string[] paths, string printerName)
{
    Process process = new Process();
    process.StartInfo.FileName = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";
    process.StartInfo.UseShellExecute = false;
    foreach (string file in paths)
    {
        string args = string.Format("/t \"{0}\" \"{1}\"", file, printerName);
        process.StartInfo.Arguments = args;
        process.Start();
    }
}
Advertisements

0 Responses to “Print a PDF in c#”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: