Skip to main content

How to Print PDF Documents

  • 6 minutes to read

The PDF Viewer control allows you to print a loaded document either by using built-in UI elements/commands or programmatically. This topic describes in detail how to:

  • Add the capability to print PDF documents in your application.

  • Print a document by using the Print dialog.

  • Print a document without end-user interaction.

How to Enable the Capability to Print PDF Documents

Enabling the capability to print the PDF Viewer control content in your application is similar to enabling the printing functionality in other DevExpress VCL controls (by using the ExpressPrinting System). As a result, you need to perform the following actions either at design- or runtime (before printing the loaded PDF document):

  • Create an instance of the TdxComponentPrinter component;

  • Create a PDF Viewer report link instance and associate it with the newly created component printer.

For detailed information on enabling the printing functionality for the PDF Viewer at design time, refer to the Control Printing Basics topic.

To associate a report link with the component printer and the PDF Viewer control at runtime, you need to use the report link’s ComponentPrinter and Component properties, respectively:

AComponentPrinter: TdxComponentPrinter;
//...
var
  AReportLink: TdxPDFViewerReportLink;
begin
  if(AComponentPrinter <> nil) then Exit;
  AReportLink := TdxPDFViewerReportLink.Create(Self);  // Create a new PDF Viewer report link instance passing the main application form as the owner
  AComponentPrinter := TdxComponentPrinter.Create(Self);  // Create a new component printer instance passing the main application form as the owner
  AReportLink.Component := dxPDFViewer1;  // Associate the report link with the PDF Viewer control
  AReportLink.ComponentPritner := AComponentPrinter;  // Associate the report link with the component printer
end;

Then, you can print the loaded PDF document either by using the Print dialog or programmatically.

How to Print a PDF Document by using the Print Dialog

The PDF Viewer uses a custom Print dialog that includes the print preview area and printed page layout settings (usually available on the Page and Scaling tabs of the Page Setup dialog) in addition to the functionality provided by the standard Print dialog used by other DevExpress VCL controls:

The Print dialog also includes the special “Print as Image” option corresponding to the PDF Viewer report link’s PrinterPage.PrintAsImage property that you can use to overcome printing issues that may occur as a result of:

  • Using an outdated printer driver;

  • Corrupted or incomplete font data in the printed document.

If the printing functionality is enabled, the PDF Viewer allows end-users to invoke the Print dialog for a loaded document by:

  • Pressing the Ctrl+P key combination;

  • Clicking the “Print…” item in the PDF Viewer’s context menu:

In addition to these options available out-of-the-box, you can link the ShowPrintForm command to an element of your UI or employ the automatic UI generation functionality. Alternatively, you can call the ShowPrintDialog global procedure in the OnClick event handler of your UI element.

All the techniques above of displaying the Print dialog are available only if one of the following conditions is met:

  • The loaded PDF document is not encrypted;

  • An encrypted PDF file is loaded by using the owner password;

  • An encrypted PDF file loaded with the user password imposes no restrictions on the document printing capability.

To ignore the printing functionality restrictions in encrypted files (i.e., when the PDF Viewer’s Document.AllowPrinting property returns False), you can call methods provided by the report link and/or component printer to invoke the Print and Page Setup dialogs:

Report Link‘s Method

Component Printer‘s Method

Description

PageSetup

PageSetup

Invokes the Page Setup dialog, normally available to end-users from the Print dialog by clicking the Page Setup… button.

The PageSetupEx function also allows you to specify the active tab of the Page Setup dialog and change visibility of the Print Preview… and Print… buttons.

PageSetupEx

PageSetupEx

Preview

Preview

Invokes the Print dialog, similar to the Print function called with True passed as the AShowDialog parameter, since the PDF Viewer’s Print dialog includes the print preview.

Print

Print

If True is passed as the AShowDialog parameter, calling this function invokes the Print dialog. Otherwise, the entire loaded document is printed on the default printer.

How to Print a PDF Document without User Interaction

To print the loaded PDF document‘s content regardless of the current printing permissions (that is, the document’s AllowPrinting property value) without end-user interaction, you can call one of the following methods provided by the report link and/or component printer:

Report Link‘s Method Component Printer‘s Method Description
Print Print Calling this function prints the entire loaded document on the default printer without end-user interaction if False is passed as the AShowDialog parameter.
PrintEx PrintEx Prints the odd, even, or all pages of the loaded PDF document on the default printer without invoking any dialogs.
PrintPages PrintPages Prints the specified pages of the loaded PDF document on the default printer without end-user interaction.
PrintPagesEx PrintPagesEx Prints the odd, even, or all PDF document pages from the specified list on the default printer without end-user interaction.

For instance, the following code example prints the entire loaded PDF document without invoking any dialogs:

var
  AReportLink: TdxPDFViewerReportLink;
//...
  if((AComponentPrinter = nil) or (AComponentPrinter.LinkCount = 0)) then Exit;  // Do not execute the following code if the printing functionality is disabled
  AReportLink := AComponentPrinter.CurrentLink as TdxPDFViewerReportLink;
  AReportLink.Print(False);  // Print the entire document loaded by the PDF Viewer without showing the Print dialog