生成自动打印的PDF

时间:2011-05-29 13:31:30

标签: c# asp.net pdf itextsharp

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

5 个答案:

答案 0 :(得分:12)

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

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

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

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

作为旁注,通过在文档类中将属性“AutoPrint”设置为TRUE({em>通常的免责声明适用),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与显示文件的阅读器进行通信。
我在this 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文件。

答案 1 :(得分:4)

此网站上的另一个解决方案......我使用此解决方案并且工作得很好

我有来自Crystal报告的PDF Stream,我用pdfsharp

添加了openaction

link: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;
}

答案 2 :(得分:3)

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

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

免责声明:我为Gnostice工作。

答案 3 :(得分:1)

在iTextSharp中执行:

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));

答案 4 :(得分:0)

如何做好旧时尚的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);
    }
}
相关问题