通过几天的查找经测试后发现以下三种方法可以实现用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的界面,但是只能实现打印功能,不能另存。