News Category

Print PDF files in C#

2014-09-22 09:10:35 Written by  support iceblue
Rate this item
(0 votes)

PDF files can't be edited easily and for this reason, it is the most popular file format in business field. Printing PDF files becomes a widely asked requirement as a result. This tutorial focus on introducing how to print PDF files via a .NET PDF API. With the help of Spire.PDF for .NET, developers can finish the print function in a few lines codes to print the PDF files with the default printer or any other network connected printer. You can also print all the PDF pages or only print the selected pages you want.

ATTENTION THAT, if you are using the Spire.PDF Version 3.9.360 or above, please refer to tutorial here.

Step 1: Create a new PDF document and load a PDF from file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");

If you want to print all the pages in PDF file with the default printer, please go to Step 2. If you want to set the printer and only print some pages in the PDF file, please go to Step 3 directly.

Step 2: Print the PDF file with the default printer to print all the pages

doc.PrintDocument.Print();

Step 3: Set the Printer and select the pages you want to print in the PDF file.

PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
          
if (dialogPrint.ShowDialog() == DialogResult.OK)
   {
     //Set the pagenumber which you choose as the start page to print
     doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
     //Set the pagenumber which you choose as the final page to print
     doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
     //Set the name of the printer which is to print the PDF
     doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

     PrintDocument printDoc = doc.PrintDocument;
     printDoc.Print();
  }

By using the Step 2 method to print all the pages with the default printer, it will start to print the PDF files automatically when you process it. If you select the printer and the pages you choose to print, then you will get a printer dialog as below:

How to print PDF files in C#

The full codes of how to print PDF file in C#:

[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.Windows.Forms;
using Spire.Pdf.Annotations;
using Spire.Pdf.Widget;
using System.Drawing.Printing;


namespace PrintPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("sample.pdf");

            //Use the default printer to print all the pages
            //doc.PrintDocument.Print();

            //Set the printer and select the pages you want to print

            PrintDialog dialogPrint = new PrintDialog();
            dialogPrint.AllowPrintToFile = true;
            dialogPrint.AllowSomePages = true;
            dialogPrint.PrinterSettings.MinimumPage = 1;
            dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
            dialogPrint.PrinterSettings.FromPage = 1;
            dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
          
            if (dialogPrint.ShowDialog() == DialogResult.OK)
            {
                doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
                doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
                doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

                PrintDocument printDoc = doc.PrintDocument;
                printDoc.Print();
            }

        }
    }
}

Additional Info

  • tutorial_title: Print PDF files in C#
Last modified on Sunday, 26 September 2021 02:20