Tuesday, 16 July 2013

Print pdf document in C#

This technical tip shows how to print a PDF document in program. Spire.Pdf allows you to print PDF document for your requirement at runtime with.NET. By using  Spire.PDF for .NET ,which is suitable for Console platform, WinForm ,and Asp.net ,you can easily load Pdf into these programs and print out this PDF document with the help of printDialog object. The printing mode of this component support background printing mode without intervention and interaction. According to Spire.pdf, you can freely switch printer, select print page, and set relative properties while printing PDF files in C#

In this blog , I will take console platform as an example to share my tips about how to print a PDF document in C#. Here we go :

Procedures:

 Step1: For printing a PDF document, firstly I created a new project in VS .Then set its target framework to be .NET framework 2.0.

 Step2: I instantiated an Spire.Pdf. PdfDocument object and load PDF document by calling its method LoadFromFile:

            // Instantiate an object of PdfDocument
                PdfDocument doc = new PdfDocument();  
           
                string pdfFile = “sample.pdf”;

              //load PDF document
                doc.LoadFromFile(pdfFile);              

                //Default printing mode without interaction (default printer to print all pages of the document)

Step3:  After what I mentioned above, it’s time for calling the method of PdfDocumen.PrintDocument.Print(). By using default printer, printing this PDF document of all pages can be realized without interaction. Besides printing PDF , i am happy to tell you that this printing mode is also suitable for Web printing in C# :

                  C#

                 doc.PrintDocument.Print();

                //Interaction printing mode(page range,printer,and properties of printing can be freely to selected and set)



Step4 :  The final step to finish printing PDF was a little complicated in C#, because i need instantiate an object of printDialog and set its relative printing properties before calling its method.   Besides ,  at the same time,set Spire.Pdf.pdfDocument relative printing properties as well. Here,the key point to print PDF ,which is what I extremely want to emphasize, is the necessity of combination between printDialog object and PdfDocument.PrintDocument while in this process. Without printDialog object, to print PDF is in vain . At last i just can call method PdfDocument.PrintDocument.print to start print PDF document:

 
                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;



The Entire Code Snippet:

Please take a look over the following code snippet for this approach:

C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing.Printing;

using Spire.Pdf;

namespace PdfPrint

{

class Program

{

[STAThread()]

static void Main(string[] args)

{

// Instantiate an object of PdfDocument

PdfDocument doc = new PdfDocument();

string pdfFile = “sample.pdf”;

//load PDF document

doc.LoadFromFile(pdfFile);

//Default printing mode without interaction (default printer to print all pages of the document)

doc.PrintDocument.Print();

//Interaction printing mode(page range,printer,and properties of printing can be freely to selected and set)

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)

{

//the startpage was selected to be printed

doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;

//the endpage was selected to be printed

doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;

//Choose printer to print pdf document

doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

PrintDocument printDoc = doc.PrintDocument;

dialogPrint.Document = printDoc;

//Realize printing

printDoc.Print();

}
}
}
}

No comments:

Post a Comment