生成自动打印的PDF

本文关键字:PDF 打印 | 更新日期: 2023-09-27 17:57:51

我有一个生成PDF的ASP.NET Web应用程序。我正在使用iTextSharp。结果是,你点击一个按钮,它就会下载。我的雇主希望能够点击按钮并通过打印对话框打开它。

生成自动打印的PDF

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

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

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

它在C#中不应该太不同。

顺便说明一下,Amyuni PDF Creator.Net也可以通过在文档类中将属性"自动打印"设置为TRUE来实现这一点(通常的免责声明适用)。

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问题中发现了另一种可能值得尝试的方法:

<html>
<script language="javascript">
timerID = setTimeout("exPDF.print();", 1000);
</script>
<body>
<object id="exPDF" type="application/pdf" data="111.pdf" width="100%" height="500"/>
</body>
</html>

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

此网站上的另一个解决方案。。。我使用这个解决方案,工作很好

我有一个来自Crystal的PDF流报告,我用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。

免责声明:我为诺斯替工作。

在iTextSharp do:中

PdfDocument = pdfDoc = new Document(PageSize.LETTER);
// create and open the new pdf document for writing
FileStream fspdfDoc = new FileStream(pdfDocFileName, FileMode.Create, 
FileAccess.ReadWrite);
PdfWriter pdfW = PdfWriter.GetInstance(pdfDoc, fspdfDoc);
pdfDoc.Open();
pdfW.AddJavaScript(PdfAction.JavaScript("this.print(true);'r", pdfW));

好的老式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);
    }
}