Printing PDF's using Adobe AcroLib 8.1

Environment: C#, VB, C++ .NET

The development world is full of tools to print to a PDF format from another type of document, and it's really just a matter of pointing to the PDF printer driver and printing the document.  When it comes to printing an existing PDF document to a windows printer it can be another matter.  Sure there are several librarys available but they tend to be very expensive, and some only encapsulate the Adobe AcroLib PDF Library anyway, so why not go ahead and use the built in API that Adobe provides.

First you must install the free Adobe Reader, and the example here is using version 8.1.2.  Then you must open your VB or C# project and add a reference to the AxAcroPDFLib.AxAcroPDF library.  Once you have added the reference go ahead and put the control on a Form.  I generally use a hidden form that contains the control.

To use the library call the .LoadFile() method and pass the file name and path, then call PrintAll() to print the document(s).  In the example below I use a Wait() method to give the file time to load into the control.  Timing can be critical when using the AxAcroPDF library and you want to insure that you are giving Adobe Reader enough time to do it's work before assigning another task.  

In previous versions of Adobe Reader libraries (7.xxx) the Adobe Reader window would stay hidden when doing a silent print job.  Since 8.1 the Adobe Reader window insists on popping up and being visible when using the library to perform a print job.  This effect can be mitigated by using the Win32 ShowWindow() function and moving it out of the view port, or minimizing it.

For more information on the Adobe API please visit www.adobe.com.  Adobe has extensive (and cryptic) documentation on the libraries that get you in the back door on Adobe Reader.

    /******************************************************************************************
    *  PrintPDF()   Prints PDF with supplied file name
    *
    *  PARAMS   [string] fileName, path and name of PDF file to print
    ******************************************************************************************/
    public void PrintPDF(string fileName)
    {
        try
        {
            axAcroPDF.LoadFile(fileName); //load the PDF into control
            Wait(5); //make sure we get the file loaded
            axAcroPDF.printAll();
            Wait(5); //give pdf time to get into the print queue
        }
        catch(Exception e)
        {
            MessageBox.Show("An error ocurred printing PDF. " + e.Message, 
                            MessageBoxButtons.OK, Application.ProductName);
        }
    }

    /******************************************************************************************
    *  Wait()   Waits specified number of seconds before returning
    *
    *  PARAMS   [int] seconds, number of seconds to wait before returning
    ******************************************************************************************/
    private void Wait(int seconds)
    {
        DateTime start = DateTime.Now;
        while (start.AddSeconds(seconds) >= DateTime.Now)
        {
            Application.DoEvents();
        }
    }