windowsforms-115860-controls-and-libraries-pdf-viewer-printing.md
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.
The Print dialog allows you to choose the printer and specify the following printing parameters:
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.
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);
}
Imports System
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports DevExpress.Pdf
Imports DevExpress.XtraPdfViewer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' Create a PDF Viewer instance and load a PDF
Dim pdfViewer As PdfViewer = Me.pdfViewer1
pdfViewer.LoadDocument("..\..\Demo.pdf")
' If required, declare and specify the system printer settings
Dim printerSettings As 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
Dim pdfPrinterSettings As New PdfPrinterSettings(printerSettings)
' Specify the PDF printer settings
pdfPrinterSettings.PageOrientation = PdfPrintPageOrientation.Auto
pdfPrinterSettings.PageNumbers = New Integer() { 1, 3, 4, 5 }
pdfPrinterSettings.ScaleMode = PdfPrintScaleMode.CustomScale
pdfPrinterSettings.Scale = 90
' Print the document
pdfViewer.Print(pdfPrinterSettings)
End Sub
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);
}
Private Sub PrintSelectedPages(ByVal sender As Object, ByVal e As ItemClickEventArgs)
Dim pages = pdfViewer1.GetSelectedThumbnailPageIndexes()
Dim printerSettings As New PdfPrinterSettings()
printerSettings.PageNumbers = pages.ToArray()
pdfViewer1.Print(printerSettings)
End Sub
The PDF Viewer ships with the following events that allow you to customize the print output:
PdfViewer.QueryPageSettingsOccurs before the page is printed and allows you to specify print settings for a specific page.PdfViewer.PrintPageOccurs 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));
}
AddHandler pdfViewer.PrintPage, AddressOf OnPrintPage
AddHandler pdfViewer.QueryPageSettings, AddressOf OnQueryPageSettings
'...
Private Shared Sub OnQueryPageSettings(ByVal sender As Object, ByVal e As PdfQueryPageSettingsEventArgs)
' Print the second page in landscape size.
If e.PageNumber = 2 Then
e.PageSettings.Landscape = True
Else
e.PageSettings.Landscape = False
End If
End Sub
' Specify what happens when the PrintPage event is raised.
Private Shared Sub OnPrintPage(ByVal sender As Object, ByVal e As PdfPrintPageEventArgs)
' Draw a picture on each printed page.
Using image As New Bitmap("..\..\DevExpress.png")
e.Graphics.DrawImage(image, New RectangleF(10, 30, image.Width \ 2, image.Height \ 2))
End Using
End Sub
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.
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.
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 RenderingEngine property is set to DirectX).
The PrintTextAsOutlines property has no effect if the EnableLegacyPrinting property is set to true.
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: