DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

How to print PDF document in C#

Spire.PDF has a powerful function to print PDF document. This article shows how to print a PDF file using C# via the following print situation:

  • Print PDF to default printer.
  • Print PDF to virtual printer.
  • Print PDF to a specified printer and select some pages in the PDF file to print.
  • Print PDF in greyscale.
  • Print Multiple PDF Pages per Sheet
  • Print Single PDF Page to Multiple Sheets

Print PDF to default printer and print all the pages on the PDF document.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
pdf.Print(); 

Print PDF to virtual printer (Microsoft XPS Document Writer).

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
pdf.PrintSettings.PrinterName = "Microsoft XPS Document Writer";   
pdf.PrintSettings.PrintToFile("PrintToXps.xps");
pdf.Print();

Print PDF to a specified printer and select some pages in the PDF file to print.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
//Set the printer 
pdf.PrintSettings.PrinterName = "HP LasterJet P1007";
//Only print the second and fourth page
pdf.PrintSettings.SelectSomePages(new int[] { 2,4 });
//Print the pages from 1 to 15
pdf.PrintSettings.SelectPageRange(1,15);
pdf.Print();

Print PDF in greyscale.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
pdf.PrintSettings.Color = false;
pdf.Print();

Print Multiple PDF Pages per Sheet. Uses can invoke the SelectMultiPageLayout(int rows, int columns) method of the PdfPrintSettings class to print multiple PDF pages per sheet of paper.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
//Print two PDF pages per sheet
pdf.PrintSettings.SelectMultiPageLayout(1, 2);
pdf.Print();

Print Single PDF Page to Multiple Sheets. The SelectSplitPageLayout() method of the PdfPrintSettings class can be used when printing a large PDF page to multiple sheets. This method splits the PDF page to multiple pages according to the standard A4 paper size: 595pt*842pt.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");
//Print the PDF page to multiple sheets
pdf.PrintSettings.SelectSplitPageLayout();
pdf.Print();

Top comments (1)

Collapse
 
peterfloyd profile image
peter Floyd

Absolutely! This guide is fantastic for simplifying PDF printing in C#. If you're looking to take your PDF handling to the next level, ZetPDF.com offers a comprehensive suite of tools and resources.