简体   繁体   English

打印使用itextsharp创建的PDF

[英]Print PDF Created using itextsharp

My Goal is to print a RDLC report on the client machine without preview. 我的目标是在没有预览的情况下在客户端计算机上打印RDLC报告。 I can not use the ReportViewer print button since it requires the installation of ActiveX object and there are no permissions for that. 我不能使用ReportViewer打印按钮,因为它需要安装ActiveX对象,并且对此没有权限。 So, I'm using ITextSharp to create a PDF from the byte array returned from the rendered LocalReport, and add a JavaScript for print. 因此,我正在使用ITextSharp从呈现的LocalReport返回的字节数组中创建PDF,并添加JavaScript进行打印。

During Debug, I can see that the PDF is generated and has 2 pages, and everything looks OK. 在调试期间,我可以看到生成了PDF,并且有2页,并且一切正常。 I don't receive any errors and the function exits OK, but it doesn't print. 我没有收到任何错误,并且该函数退出“确定”,但没有打印出来。 What am I doing wrong, or what am I missing? 我做错了什么,或者我想念什么?

This is my code: 这是我的代码:

string jsPrint = "var pp = this.getPrintParams();pp.interactive= pp.constants.interactionLevel.silent;this.print(pp);";

byte[] bytes = report.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);

using (MemoryStream ms = new MemoryStream())
{
    Document doc = new Document();

    PdfWriter writer = PdfWriter.GetInstance(doc, ms);

    doc.SetPageSize(PageSize.A4);

    doc.Open();

    PdfContentByte cb = writer.DirectContent;

    PdfImportedPage page;

    PdfReader reader = new PdfReader(bytes);

    int pages = reader.NumberOfPages;

    for (int i = 1; i <= pages; i++)
    {
        doc.SetPageSize(PageSize.A4);

        doc.NewPage();

        page = writer.GetImportedPage(reader, i);

        cb.AddTemplate(page, 0, 0);
    }

    PdfAction jAction = PdfAction.JavaScript(jsPrint, writer);

    writer.AddJavaScript(jAction);

    doc.Close();
}

Thanks. 谢谢。

Regarding your question about PdfStamper (in the comments). 关于您有关PdfStamper的问题(在评论中)。 It should be as simple as this: 它应该像这样简单:

string jsPrint = "var pp = this.getPrintParams();pp.interactive= pp.constants.interactionLevel.silent;this.print(pp);";
PdfReader reader = new PdfReader(bytes);
MemoryStream stream = new MemoryStream();
PdfStamper stamper = new PdfStamper(pdfReader, stream);
stamper.Writer.AddJavaScript(jsPrint);
stamper.Close();
reader.Close();

Regarding your original question: automatic printing of PDF documents is considered being a security hazard: one could send a PDF to an end-user and that PDF would cause the printer to spew out pages. 关于您的原始问题:自动打印PDF文档被认为是一种安全隐患:可以将PDF发送给最终用户,而PDF将导致打印机喷出页面。 That used to be possible with (really) old PDF viewers, but modern viewers prevent this from happening. 过去(确实)是旧的PDF查看器可以做到这一点,但是现代的查看器可以防止这种情况的发生。

In other words: you may be trying to meet a requirement of the past. 换句话说:您可能正在尝试满足过去的要求。 Today's PDF viewers always require an action from the end user to print a PDF document. 当今的PDF查看器始终要求最终用户采取措施来打印PDF文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM