Example T334688
Visible to All Users

PDF Document API - Customize PDF Print Output

This example shows how to customize the print output and change settings for a page to be printed.

To specify the output to print, handle PdfDocumentProcessor.PrintPage event and use the PdfPrintPageEventArgs.Graphics property. For example, to draw an image on each page, call the Graphics.DrawImage method.

You can print each document page using different page settings. For example, to print the second page in landscape size set the PdfQueryPageSettingsEventArgs.PageSettings.Landscape property to true when the PdfDocumentProcessor.QueryPageSettings event is handled.

Files to Look At

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

CustomizePrintSettings/Program.cs(vb)
C#
using DevExpress.Drawing; using DevExpress.Pdf; using System; using System.Drawing; using System.IO; namespace CustomizePrintSettings { class Program { static void Main(string[] args) { // Create a PDF Document Processor instance and load a PDF into it. using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor()) { documentProcessor.LoadDocument(@"..\..\..\Demo.pdf"); // Declare the 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 with the landscape orientation. 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. byte[] imageBytes = File.ReadAllBytes(@"..\..\..\DevExpress.png"); string imageBase64 = Convert.ToBase64String(imageBytes); using (DXImage image = DXImage.FromBase64String(imageBase64)) e.Graphics.DrawImage(image, new RectangleF(10, 30, image.Width / 2, image.Height / 2)); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.