Skip to main content

Print Documents in PDF Viewer

  • 6 minutes to read

This document describes how to print a document and customize print settings.

The Print dialog allows you to print the current document as a PDF file. Click the Print button in the File toolbar button group or context menu, or press CTRL+P to invoke it.

pdf-viewer-print

The Print dialog allows you to choose the printer and specify the following printing parameters:

  • The number of copies
  • The range of pages to print
  • Page size and orientation
  • Paper source

pdf-viewer-3

To specify the required printer settings before the Print dialog is shown, handle the PdfViewer.PageSetupDialogShowing event.

Use the PdfViewer.ShowPrintStatusDialog property to hide the print status dialog.

To print a document without invoking the Print dialog, call the corresponding overload of the PdfViewer.Print method and pass the printer settings (represented by the PdfPrinterSettings object) to this method as a parameter.

This example shows how to print a document with custom printer settings.

View Example

using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using DevExpress.Pdf;
using DevExpress.XtraPdfViewer;
//...


private void Form1_Load(object sender, EventArgs e) {
    // Create a PDF Viewer instance and load a PDF into it.
    PdfViewer pdfViewer = this.pdfViewer1;
    pdfViewer.LoadDocument(@"..\..\Demo.pdf");

    // If required, declare and specify the system printer settings
    PrinterSettings printerSettings = new PrinterSettings();
    printerSettings.PrinterName = "Microsoft XPS Document Writer";
    printerSettings.PrintToFile = true;
    printerSettings.PrintFileName = @"..\..\Demo.xps";

    // Declare the PDF printer settings.
    // If required, pass the system settings to the PDF printer settings constructor
    PdfPrinterSettings pdfPrinterSettings = new PdfPrinterSettings(printerSettings);

    // Specify the PDF printer settings
    pdfPrinterSettings.PageOrientation = PdfPrintPageOrientation.Auto;
    pdfPrinterSettings.PageNumbers = new int[] { 1, 3, 4, 5 };
    pdfPrinterSettings.ScaleMode = PdfPrintScaleMode.CustomScale;
    pdfPrinterSettings.Scale = 90;

    // Print the document
    pdfViewer.Print(pdfPrinterSettings);
}

The PDF Viewer allows you to retrieve the indexes of the pages selected by the user in the Page Thumbnails panel. Call the PdfViewer.GetSelectedThumbnailPageIndexes() method to obtain page indexes and assign the resulting collection to the PdfPrinterSettings.PageNumbers property as shown in the following example:

void PrintSelectedPages(object sender, ItemClickEventArgs e) {
    var pages = pdfViewer1.GetSelectedThumbnailPageIndexes();
    PdfPrinterSettings printerSettings = new PdfPrinterSettings();
    printerSettings.PageNumbers = pages.ToArray();
    pdfViewer1.Print(printerSettings);
}

Printing Events

The PDF Viewer ships with the following events that allow you to customize the print output:

PdfViewer.QueryPageSettings
Occurs before the page is printed and allows you to specify print settings for a specific page.
PdfViewer.PrintPage
Occurs when the page is printed.

The code snippet below handles the QueryPageSettings and PrintPage events to specify the landscape orientation for a second page and add an image on each printed page.

pdfViewer.QueryPageSettings += PdfViewer_QueryPageSettings;
pdfViewer.PrintPage += OnPrintPage;
//...

private void PdfViewer_QueryPageSettings(object sender, PdfQueryPageSettingsEventArgs e)
{
    // Print the second page in landscape size.
    if (e.PageNumber == 2)
    {
        e.PageSettings.Landscape = true;
    }
    else e.PageSettings.Landscape = false;
}
private static void OnPrintPage(object sender, PdfPrintPageEventArgs e) {

    // Draw a picture on each printed page.
    using (Bitmap image = new Bitmap(@"..\..\DevExpress.png"))
        e.Graphics.DrawImage(image, new RectangleF(10, 30, image.Width / 2, image.Height / 2));
}

Troubleshooting: Optimize the Printing Process

The PDF Viewer uses DirectX to render PDF files and the XPS API to print them. If your printer uses a PCL6 or PostScript driver, the DirectX engine converts the print job from XPS to the format used in the printer, which may reduce printing performance. We recommend that you use the XPS printer driver to optimize the printing process.

Important

Make sure that the printer driver and OS on your machine are updated to the latest version.

Common Issues

Try one of the following solutions if issues occur with the print output or an exception is thrown:

  • Set the PdfViewer.RenderingEngine property to GdiPlus to disable the DirectX printing engine and use GDI+ instead. Please note that the GDI+ engine has limitations and some content may be lost (that is, stroke and clip text rendering, transparency, and blend modes).

  • Use the legacy printing engine (the PdfPrinterSettings.EnableLegacyPrinting option). In this case, PDF pages are printed as images. The legacy printing engine uses the GDI Print API to print files, and the rendering engine specified by the RenderingEngine property to render pages as images. Note that printing performance may be reduced due to the size of printed images.

Font Embedding Issues

The PDF printing engine sends the font program with glyph codes to the printer to speed up the printing process and reduce the print job size. Set the PdfPrinterSettings.PrintTextAsOutlines property to true to convert text to character outlines. This step may help to resolve issues with incorrect font embedding for some printers. When you use this property, take into account the following:

  1. The PrintTextAsOutlines property works only with the DirectX printing engine (the RenderingEngine property is set to DirectX).

  2. The PrintTextAsOutlines property has no effect if the EnableLegacyPrinting property is set to true.

Get More Help

If these steps did not resolve issues with your document, please contact our DevExpress Support Team and include the following information in your ticket so that we can find a solution as quickly as possible:

  • The document you wish to print
  • The code snippet you utilize to print a file
  • The printer name and driver version
  • Operating system
  • DevExpress product version
  • Call stack (if available)