生成PDF自动打印 [英] Generate a PDF that automatically prints

查看:603
本文介绍了生成PDF自动打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成一个PDF ASP.NET Web应用程序。我使用iTextSharp的。什么情况是,你点击一个按钮,它的下载。我的雇主希望能够按一下按钮,并将它与打印对话框打开。

I have a ASP.NET Web application that generates a PDF. I am using iTextSharp. What happens is that you click a button and it downloads. My employer want to be able to click the button and have it open with the print dialog.

推荐答案

方法1:使用嵌入您的PDF文件里面的javascript
您可以尝试创建一个 iText的PDFAction 对象,一个javascript调用 this.print(假)(你可以使用新PdfAction(PdfAction.PRINTDIALOG)此),并将其与<一个关联href=\"http://www.javadocexamples.com/com/lowagie/text/pdf/PdfWriter/setOpenAction%28PdfAction%20action%29.html\"相对=nofollow> OpenAction事件 PDF文件的。

Method 1: Using embedded javascript inside your PDF files You can try creating an iText PDFAction object with a javascript call this.print(false) (you can use new PdfAction(PdfAction.PRINTDIALOG) for this), and associate it with the OpenAction event of your pdf file.

在iText的Java中的code应该是这样的:

The code in iText Java should look like this:

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

这不应该是在C#太diferent。

It should not be too diferent in C#.

作为一个方面说明,这也是可能的 Amyuni PDF格式制作的.Net 按文档类属性自动打印设置为TRUE(通常免责声明适用的)。

As a side note, this is also possible with Amyuni PDF Creator .Net by setting the attribute "AutoPrint" to TRUE in the document class (usual disclaimer applies).

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文件,它有,如果该文件保存在本地,每次该文件后打开它会显示打印对话框的缺点。

This approach requires the PDF file to be opened in a reader that will take care of printing, and it has the drawback that if the file is saved locally, every time the file is opened later on it will show the print dialog.

方法二:从浏览器使用JavaScript与显示文件读者沟通结果
我发现在这另一种方法<一href=\"http://stackoverflow.com/questions/5501182/print-pdf-at-clientside-browser-without-opening-pdf\">this SO 疑问,可能值得尝试:

Method 2: Using javascript from the browser to communicate with the reader that shows the file.
I found this other approach in this SO question that might worth trying:

<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文件。

The idea is to use javascript in the browser to instruct the PDF reader to print the file. This approach will work on PDF files embedded in a HTML page.

这篇关于生成PDF自动打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆