Posts Tagged 'Print PDF C#'

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();
    }
}