本文介绍了打印PDF使用iTextSharp的创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是打印的客户机上的RDLC报告没有preVIEW。因为它需要ActiveX对象的安装,也有对没有任何权限,我不能使用的ReportViewer打印按钮。所以,我使用iTextSharp的创建从渲染LocalReport返回的字节数组一个PDF,并添加JavaScript打印。

My Goal is to print a RDLC report on the client machine without preview. I can not use the ReportViewer print button since it requires the installation of ActiveX object and there are no permissions for that. So, I'm using ITextSharp to create a PDF from the byte array returned from the rendered LocalReport, and add a JavaScript for print.

调试过程中,我可以看到生成的PDF,具有2页,并且一切看起来OK。我没有收到任何错误和函数退出确定,但它不打印。我在做什么错了,还是我缺少什么?

During Debug, I can see that the PDF is generated and has 2 pages, and everything looks OK. 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?

这是我的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();
}

感谢。

推荐答案

关于你提到的关于 PdfStamper 的问题(在评论)。它应该是像这样简单:

Regarding your question about PdfStamper (in the comments). 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();

关于你的原题:PDF文档的自动打印被认为是一个安全隐患:人们可以在PDF发送给最终用户和PDF会导致打印机喷涌出来的页面。这曾经是可能的(真的)旧PDF浏览器,而是从现代发生观众prevent这一点。

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. That used to be possible with (really) old PDF viewers, but modern viewers prevent this from happening.

在换句话说:你可能试图迎了过去的要求。今天的PDF查看器总是需要从最终用户的操作打印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使用iTextSharp的创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:07