(轉)C# 打印PDF文件使用第三方DLL


  本文為轉載,原文:http://www.cnblogs.com/Yesi/p/5066835.html

     DLL地址:https://freepdf.codeplex.com

      

下面是該解決方案的詳細代碼分步解析:

第一步:添加項目引用。

安裝控件后,創建一個新的項目,找到控件的安裝目錄,在項目的“解決方案”窗口右擊->添加引用,選擇和項目.NET Framework版本對應的dll文件進行添加;

如下圖:

                     

第二步:使用命名空間。

在該方案中,我使用的命名空間如下:

1
2
3
using Spire.Pdf;
using System.Windows.Forms;
using System.Drawing.Printing;

 

第三步:創建一個新的PDF文檔,並加載待打印的PDF文件。

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

如果需要使用默認打印機打印所有頁面,請看第四步。如果需要使用其他打印機並設置打印頁面范圍,請看第五步。

 

第四步:使用默認打印機打印所有頁面。

1
doc.PrintDocument.Print();

 

第五步:選擇打印機和設置打印頁面范圍。 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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();
     }

 

運行項目,輸出的效果圖如下(打印機和打印頁面范圍可以自己選擇):

 

 

全部代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Spire.Pdf;
using System.Windows.Forms;
using System.Drawing.Printing;
 
namespace PrintPDF
{
     class Program
     {
         static void Main( string [] args)
         {
             PdfDocument doc = new PdfDocument();
             doc.LoadFromFile( "sample.pdf" );
  
             //選擇默認打印機打印所有頁面
             //doc.PrintDocument.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;
                 dialogPrint.Document = printDoc;
                 printDoc.Print();
             }
         }
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM