Printing PDF to an XPS Printer
The following code snippet also work with Aspose.PDF.Drawing library.
Print PDF to XPS printer in C#
You can print a PDF file to an XPS printer, or some other soft printer for that matter, using the PdfViewer class. In order to do that, create an object of the PdfViewer class and open the PDF file using the BindPdf method. You can set different print settings using the PrinterSettings and PageSettings classes. You also need to set the PrinterName property to the XPS or other soft printer you have installed.
Finally, use PrintDocumentWithSettings method to print the PDF to XPS or other soft printer. The following code snippet shows you how to print the PDF file to an XPS printer.
public static void PrintToXpsPrinter()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(_dataDir + "input.pdf");
// Set attributes for printing
viewer.AutoResize = true; // Print the file with adjusted size
viewer.AutoRotate = true; // Print the file with adjusted rotation
viewer.PrintPageDialog = false; // Do not produce the page number dialog when printing
// Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
// Set XPS/PDF printer name
ps.PrinterName = "Microsoft XPS Document Writer";
// Or set the PDF printer
// Ps.PrinterName = "Adobe PDF";
// Set PageSize (if required)
pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
// Set PageMargins (if required)
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// Close the PDF file after priting
viewer.Close();
}
Choosing paper source by PDF page size
Since the 24.4 release, choosing paper source by PDF page size in the print dialog is possible. The next code snippet enables picking a printer tray based on the PDF’s page size.
This preference can be switched on and off using the ‘PdfContentEditor’ facade.
using (PdfContentEditor contentEditor = new PdfContentEditor())
{
contentEditor.BindPdf("input.pdf");
// Set the flag to choose a paper tray using the PDF page size
contentEditor.ChangeViewerPreference(ViewerPreference.PickTrayByPDFSize);
contentEditor.Save("result.pdf");
}