Printing in PDF Viewer for WPF
- 6 minutes to read
This document describes how to print a document and customize print settings.
Print PDF Files in the User Interface
You can print the current document Use the Print dialog to print a PDF file. Click the Print button in the File toolbar button group or context menu, or press CTRL+P to invoke it.
To invoke the Print dialog in code, call the PdfViewerControl.Print overload method that takes no parameters or execute the PdfViewerControl.PrintDocumentCommand command.
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
- File path (if the Print to file option is selected)
To specify the required printer settings before the Print
dialog is shown, handle the PdfViewerControl.PageSetupDialogShowing event.
Use the PdfViewerControl.ShowPrintStatusDialog property to hide the print status dialog.
Print PDF Files in Code
Call the PdfViewerControl.Print method to print a document without invoking the Print dialog. Use the PdfPrinterSettings object options to specify printer settings.
This example shows how to print a document with custom printer settings.
using System.Drawing.Printing;
using System.Windows;
using DevExpress.Pdf;
public MainWindow() {
InitializeComponent();
// Load a PDF document.
pdfViewer.OpenDocument(@"..\..\Demo.pdf");
}
private void pdfViewer_DocumentLoaded(object sender, RoutedEventArgs e) {
// 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 using the specified printer settings and show print status parameter.
pdfViewer.Print(pdfPrinterSettings, true);
}
<Window
xmlns:dxpdf="http://schemas.devexpress.com/winfx/2008/xaml/pdf"
x:Class="SpecifyPrinterSettings.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<dxpdf:PdfViewerControl x:Name="pdfViewer"
DocumentLoaded="pdfViewer_DocumentLoaded"/>
</Window>
Print Pages Selected in the Page Thumbnails Panel
The PDF Viewer allows you to retrieve the indexes of the pages selected by the user in the Page Thumbnails panel. Call the ActualThumbnailsViewerSettings.GetSelectedThumbnailPageIndexes() method to obtain page indexes and assign the resulting collection to the PdfPrinterSettings.PageNumbers property as shown in the following example:
using System.Linq;
// ...
private void simpleButton_Click(object sender, RoutedEventArgs e){
var pages = viewer.ActualThumbnailsViewerSettings.GetSelectedThumbnailPageIndexes();
PdfPrinterSettings printerSettings = new PdfPrinterSettings();
printerSettings.PageNumbers = pages.ToArray();
viewer.Print(printerSettings);
}
Printing Events
The PDF Viewer ships with the following events that allow you to customize the print output:
- PdfViewerControl.QueryPageSettings
- Occurs before the page is printed and allows you to specify print settings for a specific page.
- PdfViewerControl.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.
using DevExpress.Xpf.PdfViewer;
using System.Drawing;
//...
private void pdfViewer_QueryPageSettings(DependencyObject d, PdfQueryPageSettingsEventArgs e)
{
// Print the second page in landscape size.
if (e.PageNumber == 2)
{
e.PageSettings.Landscape = true;
}
else e.PageSettings.Landscape = false;
}
private void pdfViewer_PrintPage(DependencyObject d, 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 the 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 CompatibilitySettings.RenderPDFPageContentWithDirectX property to
false
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 RenderPDFPageContentWithDirectX property to render pages as images. Note that the 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:
The
PrintTextAsOutlines
property works only with the DirectX printing engine (the RenderingEnRenderPDFPageContentWithDirectXgine property is set totrue
).The
PrintTextAsOutlines
property has no effect if the EnableLegacyPrinting property is set totrue
.
Get More Help
If these steps did not resolve issues with your document, please contact our DevExpress Support Team and provide the following information in your ticket so we can find a solution as fast as possible:
- The document you want to print
- The code snippet you utilize to print a file
- The printer name and driver version
- Operating system
- DevExpress products version
- Call stack (if available)