Printing PDF in .NET Framework
The following code snippet also work with Aspose.PDF.Drawing library.
Print Pdf File in C# - Print PDF File to Default Printer using Printer and Page Settings
This article describes how to Print PDF File to Default Printer using Printer and Page Settings in C#.
The PdfViewer class allows you to print a PDF file to the default printer. You need to create a PdfViewer object and open the PDF using the BindPdf method. To specify different print settings, use the PageSettings
and PrinterSettings
classes. Finally, call the PrintDocumentWithSettings method to print the PDF to the default printer. The following code snippet shows how to print PDF to the default printer with printer and page Settings.
public static void SimplePrint()
{
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.BindPdf(System.IO.Path.Combine(_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();
System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
// Set printer name
ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
// 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();
}
In order to display a print dialog, try using the following code snippet:
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// Document printing code goes here
// Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
}
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 ‘Document.PickTrayByPdfSize’ property.
using (Document document = new Document())
{
Page page = document.Pages.Add();
page.Paragraphs.Add(new TextFragment("Hello world!"));
// Set the flag to choose a paper tray using the PDF page size
document.PickTrayByPdfSize = true;
document.Save("result.pdf");
}