標籤:
通過幾天的尋找經測試後發現以下三種方法可以實現用C#直接列印PDF檔案。
方法一:通過調用命令列:
using System.Drawing.Printing;using System.Diagnostics;using System.Collections.Specialized;//列印方法private void pdfPrint(string filePath){PrintDocument pd = new PrintDocument();Process p = new Process();ProcessStartInfo startInfo = new ProcessStartInfo();startInfo.CreateNoWindow = true;startInfo.WindowStyle = ProcessWindowStyle.Hidden;startInfo.UseShellExecute = true;startInfo.FileName = filePath;startInfo.Verb = "print";startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";p.StartInfo = startInfo;p.Start();p.WaitForExit();}protected void btn_print_Click(object sender, EventArgs e){string filePath="C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\1.pdf";pdfPrint(filePath);}
總結:以上方法單擊列印後會跳出一個adobe視窗,但是不會顯示任何內容,印表機會自動列印,經測試現在一個問題,在列印我公司的通告時列印出來的內容是異常的。
方法二:通過調用其他的類庫(PDFRender4NET)實現
需要引用O2S.Components.PDFView4NET.dll和O2S.Components.PDFRender4NET.dll
using O2S.Components.PDFRender4NET; /// <summary> /// 列印的代碼 /// </summary> /// <param name="url">要列印的PDF路徑</param> private int printShow(string url) { int isOK = 0; PDFFile file = PDFFile.Open(url); PrinterSettings settings = new PrinterSettings(); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); settings.PrinterName = "hp LaserJet 1160 PCL 5e"; settings.PrintToFile = false ; //設定紙張大小(可以不設定,取預設設定)3.90 in, 8.65 in PaperSize ps = new PaperSize("test",4,9); ps.RawKind = 9; //如果是自訂紙張,就要大於118,(A4值為9,詳細紙張類型與值的對照請看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx) O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings); pdfPrintSettings.PaperSize = ps; pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional; pdfPrintSettings.PrinterSettings.Copies = 1; try { file.Print(pdfPrintSettings); isOK = 1; } catch (Exception) { isOK = -1; throw; } finally { file.Dispose(); } return isOK; }
總結:以上方法單擊列印後會直接列印,不會跳出adobe的視窗。
方法三:載入adobe的com組件
1、開啟winform介面,然後在左邊的在工具列中右擊->單擊choose Items->單擊COM Components在裡面將Adobe PDF Reader勾上確定。
2、將剛載入進來的Adobe PDF Reader控制項拖到winform介面。
3、然後在載入介面輸入如下代碼:
string fileName = "C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\aa1.pdf";
this.axAcroPDF1.LoadFile(fileName);
axAcroPDF.1setShowToolbar(false);
axAcroPDF1.LoadFile(fileName);
axAcroPDF1.printAll();
另:拖過來的axAcroPDF1也可以自己建立如下代碼:
AxAcroPDFLib.AxAcroPDF axAcroPDF 1= new AxAcroPDFLib.AxAcroPDF();
axAcroPDF1.Location = new System.Drawing.Point(0, 24);
axAcroPDF1.Size = new System.Drawing.Size(292, 242);
axAcroPDF1.Dock = DockStyle.Fill;
Controls.Add(axAcroPDF1);
總結:以上方法完成了顯示PDF檔以及列印功能,但是這樣運行後會先跳出一個提示視窗是否列印,不管你是否列印都會顯示一個無任何內容的adobe視窗,然後也會在winform中顯示PDF檔。
方法四:用第三方控制項iTextSharp複製PDF檔列印
/// <summary> /// 實現PDF複製 /// </summary> /// <param name="filePath">源PDF檔</param> /// <param name="toPath">目標c1PDF檔</param> /// <param name="print">是否實現自動列印</param> private void ConvertPDFToPDF(string filePath, string toPath, bool print) { PdfReader reader = new PdfReader(filePath); Document document = new Document(reader.GetPageSizeWithRotation(1)); int n = reader.NumberOfPages; FileStream baos = new FileStream(toPath, FileMode.Create, FileAccess.Write); PdfCopy copy = new PdfCopy(document, baos); copy.ViewerPreferences = PdfWriter.HideToolbar | PdfWriter.HideMenubar; //往pdf中寫20837 ¤J內23481 ®e document.Open(); for (int i = 1; i <= n; i++) { PdfImportedPage page = copy.GetImportedPage(reader, i); copy.AddPage(page); } if (print) { PdfAction.JavaScript("myOnMessage();", copy); copy.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert(‘Test‘,2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;"); } document.Close(); reader.Close(); }
總結:此方法是實現將PDF複製到另一個地方,然後使用者去開啟複製的PDF檔後就會直接列印,此檔也會跳出Adobe的介面,但是只能實現列印功能,不能另存。
C# 列印本地PDF檔案