Dev Direct Solution Center

For more information and to buy this product...

How to print PDF documents with PDF Reader SDK

  For a copy of the sample project please click here to download and install the product evaluation.

Introduction

With Black Ice PDF Reader SDK it is easy to automate the printing of PDF documents programatically. Supports the following languages: C++ (BiPDFReader.dll); VB, Delphi, C#, J#, VB.NET (BiPDFReader.ocx)

Detail

The PDF Reader SDK supports loading and displaying PDF documents and searching in that. Displaying means not only displaying on the monitor, one can "display" a document on a printer device as well – in other words: print it. With Black Ice PDF Reader SDK one can even develop automated PDF printing without needing a visual application. The following C++ (MFC) code snippet shows how to print a PDF document.

If one would like to use the PDF Reader SDK, one has to add the BiPDFReader.lib to the project and include the BiPDFReader.h header file.


The following lines declare a CPDF object and load a PDF file into the memory.

C++
CPDF pdf; if( !pdf.LoadPDFFromFile(PDFFileName) ){ AfxMessageBox(_T("Cannot open the PDF file!")); return; }

The next lines pop up a Printer settings dialog, get the Printer’s DC (Device Context) and set up the Document Information (DOCINFO) structure.

C++
CPrintDialog dlg(FALSE); if( dlg.DoModal() == IDOK ){ // Get a handle to the printer device context (DC). HDC hdc = dlg.GetPrinterDC(); ASSERT(hdc); // Zero and then initialize the members of a DOCINFO structure. DOCINFO di; memset( &di, 0, sizeof(DOCINFO) ); di.cbSize = sizeof(DOCINFO); di.lpszDocName = _T("PDF Printing Test"); di.lpszOutput = (LPTSTR) NULL; di.lpszDatatype = (LPTSTR) NULL; di.fwType = 0;

The following lines start printing the document and printing the pages of the document in a loop.

C++
// Begin a print job by calling the StartDoc function. if( StartDoc(hdc, &di) == SP_ERROR ){ // Clean up. CDC::FromHandle(hdc)->DeleteDC(); AfxMessageBox(_T("Error: StartDoc")); return; } for(int i=0; i// Inform the driver that the application is about to begin sending data. if( StartPage(hdc) <= 0 ){ // Clean up. CDC::FromHandle(hdc)->DeleteDC(); AfxMessageBox(_T("Error: StartPage")); return; }

The following lines set the zoom value and draw a page of the PDF document onto the Printer’s DC.

C++
// Set the zoom value float width; float height; pdf.GetPageSize(i+1, width, height); float zoomX = (float)GetDeviceCaps(hdc, HORZRES)/width; float zoomY = (float)GetDeviceCaps(hdc, VERTRES)/height;; float zoom = zoomX// Draw the page into the DC of the printer driver pdf.DrawPage(hdc, i+1, zoom);

The following lines end the page writing and the document printing, then delete the DC of the Printer.

C++
// Inform the driver that page has ended. if( EndPage(hdc) <= 0 ){ // Clean up. CDC::FromHandle(hdc)->DeleteDC(); AfxMessageBox(_T("Error: EndPage")); return; } } // Inform the driver that document has ended. if( EndDoc(hdc) <= 0 ){ AfxMessageBox(_T("Error: EndDoc")); } // Clean up. CDC::FromHandle(hdc)->DeleteDC(); AfxMessageBox(_T("Printing finished successfully.")); }

This code snippet uses the following methods of the CPDF class:

  • LoadPDFFromFile: load the PDF file into the memory
  • GetPageSize: get the size of a page of a PDF document
  • DrawPage: draw the PDF page into a DC

Using the above method it is easy to print PDF documents from an application.

Supports the following languages: C++ (BiPDFReader.dll); VB, Delphi, C#, J#, VB.NET (BiPDFReader.ocx)

You may also be interested in Black Ice PDF Printer Driver: http://www.blackice.com/Printer%20Drivers/PDF%20Printer%20Drivers.htm

Visit Black Ice Software for more information and more samples.