How to print PDF document in C#

Spire.PDF has a powerful function to print PDF document. We have done some adjustments by the code to print the PDF files. If you are using the Spire.PDF Version before 3.9.360, please refer to tutorial here for print the PDF files in C#. From Spire.PDF v 3.9.360, you should use the new method offers in this article. This example shows how to print a PDF file using C# via the following print methods:

  • 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 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();