Home > Articles > .Net Framework Articles > How to print PDF files in C#

How to print PDF files in C#

by Jane Smith   on Jun 30, 2016   Category: .Net Framework  | Level: Beginner  |  Views: 989    |  Points: 100   
Like this article? Bookmark and Share:
Print the PDF files in C# .NET and set to print the PDF files with the default printer or any other network connected printer from code.

Introduction


I got a project to help my client printing the generated PDF bills pro-grammatically in C# .NET. The problem is that there is no PDF reader software (like Adobe) installed on my client’s machine and there are more than one connected printer for their server machine. So I need to find a solution to print the PDF files silently with a choose printer. I searched a lot and finally found a free .NET PDF API, free Spire.PDF for .NET. It totally meets my requirements of print the PDF files independently and I can set to print the PDF files with the default printer or any other network connected printer from code. I can also print the whole PDF file or only print some selected pages I want.


Paragraphs

Free Spire.PDF is a free and standalone PDF controls which enables developers to create, write, edit, convert and print PDF files on any .NET applications. 

Firstly, the free PDF for .NET is listed on Codeplex, downloading and installing for Spire.PDF is simple and clean. You can get the .msi file directly after download it. When you finish the installing, you can get the "SampleCenter" and API there. If you only want to get the dlls, you can get it from Nuget , use the following commend to get it.

PM> Install-Package FreeSpire.PDF

After getting Spire.PDF dlls and add it as the reference, here comes to the steps of how to print the PDF files in C#.

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

PdfDocument doc = new PdfDocument();

doc.LoadFromFile("Sample.pdf");

Step 2: Set the Printer and select the pages I want to print from 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;

    dialogPrint.Document = printDoc;

    printDoc.Print();

}                    

After processing the project, we will get a printer dialog as below:



I can also use doc.PrintDocument.Print() method to print all the pages with the default printer, then it will start to print the PDF files automatically when I process it. 

Full codes

using Spire.Pdf;

using System.Drawing;

using System.Windows.Forms;

using System.Drawing.Printing;

namespace PDF

{

    class Program

    {

        static void Main(string[] args)

        {

            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile("Sample.pdf");

 

            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;

                dialogPrint.Document = printDoc;

                printDoc.Print();

            }

          

        }

    }

}


Conclusion


Free Spire.PDF for .NET is free and easy to use. It has no any limits on the license. But it can process at most 10 pages of PDF files. When the PDF files are more than 10 pages, it throws an error exception. Then you may need to use the purchased version of Spire.PDF.






Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts