生成自动打印的PDF

我有一个生成PDF的ASP.NET Web应用程序。 我正在使用iTextSharp。 会发生什么是您单击按钮并下载。 我的雇主希望能够单击按钮并使用打印对话框打开它。

方法1:在PDF文件中使用嵌入式javascript您可以尝试使用javascript调用this.print(false)创建iText PDFAction对象(您可以使用new PdfAction(PdfAction.PRINTDIALOG) ),并将其与OpenAction事件相关联你的pdf文件。

iText Java中的代码应如下所示:

 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("file.pdf")); ... PdfAction action = new PdfAction(PdfAction.PRINTDIALOG); writer.setOpenAction(action); ... 

它在C#中应该不会太差异。

作为旁注,通过在文档类中将属性“AutoPrint”设置为TRUE( 适用通常的免责声明 ), Amyuni PDF Creator .Net也可以实现这一点。

 acPDFCreatorLib.Initialize(); acPDFCreatorLib.SetLicenseKey("Amyuni Tech.", "07EFCDA00...BC4FB9CFD"); Amyuni.PDFCreator.IacDocument document = pdfCreator1.Document; // Open a PDF document from file System.IO.FileStream file1 = new System.IO.FileStream("test_input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read); IacDocument document = new IacDocument(null); if (document.Open(file1, "")) { //Set AutoPrint document.Attribute("AutoPrint").Value = true; //Save the document System.IO.FileStream file2 = new System.IO.FileStream("test_output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write); document.Save(file2, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView); } // Disposing the document before closing the stream clears out any data structures used by the Document object document.Dispose(); file1.Close(); // terminate library to free resources acPDFCreatorLib.Terminate(); 

这种方法需要在一个负责打印的阅读器中打开PDF文件,它的缺点是如果文件是在本地保存的,那么每次打开文件时它都会显示打印对话框。

方法2:使用浏览器中的javascript与显示文件的阅读器进行通信。
我在这个SO问题中发现了另一种可能值得尝试的方法:

       

我们的想法是在浏览器中使用javascript来指示PDF阅读器打印文件。 此方法适用于嵌入在HTML页面中的PDF文件。

这个网站上的另一个解决方案……我使用这个解决方案并且运行良

我有一个来自Crystal报告的PDF Stream,我用pdfsharp添加了openaction

链接: http : //www.vo1dmain.info/pdfsharp-howto-inject-javascript-into-pdf-autoprinting-functionality#comments

 public static MemoryStream AddAutoPrint(Stream pdfStream, bool ShowPrintDialog = true, int NumCopies = 1) { PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import); PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument(); for (int idx = 0; idx < doc.PageCount; idx++) { PdfSharp.Pdf.PdfPage p = doc.Pages[idx]; outputDocument.AddPage(p); } outputDocument.Info.Author = "author name"; string JSScript = string.Empty; JSScript += "var pp = this.getPrintParams(); "; if(NumCopies > 0) { JSScript += "pp.NumCopies = " + NumCopies.ToString() + "; "; } if(!ShowPrintDialog) { JSScript += "pp.interactive = pp.constants.interactionLevel.automatic; "; } JSScript += "this.print({printParams: pp}); "; PdfSharp.Pdf.PdfDictionary dictJS = new PdfSharp.Pdf.PdfDictionary(); dictJS.Elements["/S"] = new PdfSharp.Pdf.PdfName("/JavaScript"); //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "print(true);"); //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "this.print({bUI: false, bSilent: true, bShrinkToFit: true});"); //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "var pp = this.getPrintParams(); pp.NumCopies = 2; pp.interactive = pp.constants.interactionLevel.automatic; this.print({printParams: pp});"); dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, JSScript); outputDocument.Internals.AddObject(dictJS); PdfSharp.Pdf.PdfDictionary dict = new PdfSharp.Pdf.PdfDictionary(); PdfSharp.Pdf.PdfArray a = new PdfSharp.Pdf.PdfArray(); dict.Elements["/Names"] = a; a.Elements.Add(new PdfSharp.Pdf.PdfString("EmbeddedJS")); a.Elements.Add(PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dictJS)); outputDocument.Internals.AddObject(dict); PdfSharp.Pdf.PdfDictionary group = new PdfSharp.Pdf.PdfDictionary(); group.Elements["/JavaScript"] = PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dict); outputDocument.Internals.Catalog.Elements["/Names"] = group; MemoryStream ms = new MemoryStream(); outputDocument.Save(ms, false); return ms; } 

如yms所述,您可以生成具有JavaScript或“命名”PDF操作的PDF,该操作在打开文档时显示“打印”对话框。 我们在创建自动打印PDF文章中使用我们的产品Gnostice PDFOne .NET演示了这一点。 你可以在iText中做同样的事情。 如果Adobe Reader在浏览器中注册为PDF插件,则两个选项都可以使用。

HTML Javascript选项似乎只适用于IE。

免责声明:我为Gnostice工作。

关于良好的旧时尚OLE如何? 它仍然受到我所知道的大多数文档层的支持…在C#中我通常做这样的事情……其中PDF,RTF,DOC,XLS ……无所谓……它们都打印出来……

 public void HandlePresentation(string fullFilePath, bool viewOnScreen, bool autoPrint) { ProcessStartInfo info = new ProcessStartInfo(fullFilePath); if (autoPrint) { info.Verb = "Print"; info.WindowStyle = ProcessWindowStyle.Hidden; // not normally required Process.Start(info); info.Verb = string.Empty; } if (viewOnScreen) { info.WindowStyle = ProcessWindowStyle.Normal; Process.Start(info); } }