SoFunction
Updated on 2025-03-07

10 ways to print PDF documents in C# (summary)

Printing is one of the common requirements when operating PDF documents. According to different printing needs, it can be carried out in various situations, such as setting silent printing, specifying the printing page number range and printing paper size, double-sided printing, black and white printing, etc. After testing, the following will summarize the common PDF printing requirements below. Here are 10 printing requirements and methods. as follows:

  1. Print PDF documents using the default printer
  2. Print PDF documents using a virtual printer (Microsoft XPS Document Writer)
  3. Specify the printer and PDF document printing page range
  4. Print PDF documents silently
  5. Print PDF documents on both sides
  6. Print PDF documents in black and white
  7. Select a different output tray when printing PDF documents
  8. Print multiple copies of PDF documents
  9. Print PDF for one page as multiple pages, print multiple pages as one page
  10. Custom paper size print PDF

Usage Tool: for .NET

How to get it:

1. ByOfficial websitedownload. Add a reference to the program, and the dll file is in the bin folder under the installation path.

2. ByNuget Website download.

C# sample code

[Example 1] Print PDF using the default printer

//Create an object of the PdfDocument class and load the PDF documentPdfDocument doc = new PdfDocument();
("");

//Print all pages of the document using the default printer();

[Example 2] Print PDF using a virtual printer (Microsoft XPS Document Writer)

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Select Microsoft XPS Document Writer printer = "Microsoft XPS Document Writer";

//Print PDF document to XPS format("");
();

【Example 3】Specify the printer and PDF document printing page number range

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Specify the printer = "HP LaserJet P1007";

//Set the document printing page number range(1, 5);

//Print discontinuous pages//(new int[] { 1, 3, 5, 7 });

//Print PDF document();

【Example 4】Silently print PDF

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Silently print PDF documents = new StandardPrintController();
();

【Example 5】Double-sided printing PDF

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Discern whether the printer supports double-sided printingif ()
{
  // If supported, set the double-sided printing mode, optional: Default/Simplex/Horizontal/Vertical   = ;
  //Print PDF document  ();
}

【Example 6】Print PDF in black and white

//Load PDF documentPdfDocument pdf = new PdfDocument();
("");

//Print PDF document in black and white = false;
();

【Example 7】Select a different output tray when printing a PDF document

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Set the paper tray += delegate(object sender, PdfPaperSettingsEventArgs e)
{
  //The paper source for setting pages 1-50 is paper tray 1  if (1 <=  &&  <= 50)
  {
     = [0];
  }
  //Set the paper source for the remaining pages as paper tray 2  else
  {
     = [1];
  }
};

//Print PDF document();

[Example 8] Print multiple copies of PDF documents

//Load PDF documentPdfDocument doc = new PdfDocument();
("");

//Set the number of copies to 2 copies = 2;

//Print PDF document();

[Example 9] Print PDF for one page, multiple pages, multiple pages are one page

1. Call the SelectMultiPageLayout method of the PdfPrintSettings class to print multiple pages of a PDF document onto a piece of paper.

//Instantiate a PdfDocument objectPdfDocument pdf = new PdfDocument();
//Load PDF document("");

//Print every two pages of a PDF document onto one piece of paper (type format is 1 row, 2 columns)(1, 2);
()

2. The SelectSplitPageLayout method of the PdfPrintSettings class supports printing a single page of a PDF document on multiple sheets of paper. This method is to split the PDF page according to the standard size of A4 paper 595pt*842pt. The pages exceeding this size will be printed to the next sheet of paper when printing.

//Instantiate a PdfDocument objectPdfDocument pdf = new PdfDocument();
//Load PDF document("");

//Split and print the single page of the PDF document according to the standard page size and();
()

【Example 10】Custom paper size print PDF

1. Customize paper size printing with a virtual printer: Keep the original page size printed to PDF

//Load the PDF document that needs to be printed doc = new ();
(FileName);

//Get the paper size of the first page of the original document, the unit here is PointSizeF size = [0].Size;

//Instantiate the PaperSize object and set its width and height//It should be noted that the unit conversion is involved. The default unit of PaperSize's width and height parameter is 100 inchesPaperSize paper = new PaperSize("Custom", (int)/72*100, (int)/72*100);
 = (int);

//Set the printed paper size to the original document size = paper;

//FitSize printing mode needs to be selected(, true);
//Print();

2. Use the real printer to set the paper size in the paper tray to print: Print the original A4 document to the size of A3.

//Load the PDF document that needs to be printed  doc = new ();
 (FileName);

 PaperSize p = null;
 //Instantiate a PrintDocument object to get the current printer's tray information PrintDocument printDoc = new PrintDocument();

 //Travel through the paper inside the printer paper tray and find the required A3foreach (PaperSize ps in )
      {
        if (("A3"))
        {
          p = ps;
          break;
        }
        
      }

 //Set the printed paper size to A3  = p;

 //Print (, true);
 ();

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.