Skip to main content

PdfDocumentProcessor.PrintPage Event

Occurs when the document page is printed.

Namespace: DevExpress.Pdf

Assembly: DevExpress.Docs.v23.2.dll

NuGet Package: DevExpress.Document.Processor

Declaration

public event PdfPrintPageEventHandler PrintPage

Event Data

The PrintPage event's data class is PdfPrintPageEventArgs. The following properties provide information specific to this event:

Property Description
Cancel Gets or sets a value indicating whether the print job should be canceled. Inherited from PrintPageEventArgs.
Graphics Gets the Graphics used to paint the page. Inherited from PrintPageEventArgs.
HasMorePages Gets or sets a value indicating whether an additional page should be printed. Inherited from PrintPageEventArgs.
MarginBounds Gets the rectangular area that represents the portion of the page inside the margins. Inherited from PrintPageEventArgs.
PageBounds Gets the rectangular area that represents the total area of the page. Inherited from PrintPageEventArgs.
PageCount Returns the total number of pages which were sent to the printer.
PageNumber Returns the page number of the currently printed page.
PageSettings Gets the page settings for the current page. Inherited from PrintPageEventArgs.

Remarks

Use this event to perform actions when the document is sent to a printer. The page number of the currently printed page and the total number of pages which were sent to the printer can be accessed via the PdfPrintPageEventArgs.PageNumber and PdfPrintPageEventArgs.PageCount properties, respectively.

The print job can also be canceled by setting the Cancel property to true for the PrintPageEventArgs. For more information about other properties of PrintPageEventArgs, see the PrintPageEventArgs Class MSDN topic.

Example

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.

View Example: PDF Document API - Customize PDF Print Output

using DevExpress.Pdf;
using System.Drawing;

// Create a PDF Document Processor instance and load a PDF file
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) {
    documentProcessor.LoadDocument(@"..\..\Demo.pdf");

    // Declare PDF printer settings.
    PdfPrinterSettings settings = new PdfPrinterSettings();

    // Specify the page numbers to be printed.
    settings.PageNumbers = new int[] { 1, 2, 3, 4, 5, 6 };

    // Handle the PrintPage event to specify print output.
    documentProcessor.PrintPage += OnPrintPage;

    // Handle the QueryPageSettings event to customize settings for a page to be printed.
    documentProcessor.QueryPageSettings += OnQueryPageSettings;

    // Print the document using the specified printer settings.
    documentProcessor.Print(settings);

    // Unsubscribe from PrintPage and QueryPageSettings events. 
    documentProcessor.PrintPage -= OnPrintPage;
    documentProcessor.QueryPageSettings -= OnQueryPageSettings;
}

private static void OnQueryPageSettings(object sender, PdfQueryPageSettingsEventArgs e) {

    // Print the second page in landscape size.
    if (e.PageNumber == 2) {
        e.PageSettings.Landscape = true;
    }
    else e.PageSettings.Landscape = false;
}

// Specify what happens when the PrintPage event is raised.
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));
}

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the PrintPage event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also