【问题标题】:Print PDF from ASP.Net without preview从 ASP.Net 打印 PDF 而无需预览
【发布时间】:2010-09-21 04:54:05
【问题描述】:

我使用 iTextSharp 生成了一个 pdf,我可以在 ASP.Net 中很好地预览它,但我需要将它直接发送到打印机而不进行预览。我希望用户单击打印按钮并自动打印文档。

我知道可以使用 javascript window.print() 将页面直接发送到打印机,但我不知道如何将其制作为 PDF。

编辑:它没有嵌入,我是这样生成的;

                ...
                FileStream stream = new FileStream(Request.PhysicalApplicationPath + "~1.pdf", FileMode.Create);
                Document pdf = new Document(PageSize.LETTER);
                PdfWriter writer = PdfWriter.GetInstance(pdf, stream);
                pdf.Open();
                pdf.Add(new Paragraph(member.ToString()));
                pdf.Close();

                Response.Redirect("~1.pdf");
                ...

我在这里。

【问题讨论】:

    标签: c# asp.net pdf printing


    【解决方案1】:

    如果你使用 pdfsharp 会有点棘手,但非常可行

    PdfDocument document = new PdfDocument();
    PdfPage page = document.AddPage(); 
    XGraphics gfx = XGraphics.FromPdfPage(page); 
    XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic); 
    // Draw the text 
    gfx.DrawString("Hello, World!", font, XBrushes.Black, 
        new XRect(0, 0, page.Width, page.Height), 
        XStringFormats.Center); 
    
    // real stuff starts here
    
    // current version of pdfsharp doesn't support actions 
    // http://www.pdfsharp.net/wiki/WorkOnPdfObjects-sample.ashx
    // so we got to get close to the metal see chapter 12.6.4 of 
    // http://partners.adobe.com/public/developer/pdf/index_reference.html
    PdfDictionary dict = new PdfDictionary(document); // 
    dict.Elements["/S"] = new PdfName("/JavaScript"); // 
    dict.Elements["/JS"] = new PdfString("this.print(true);\r");
    document.Internals.AddObject(dict);
    document.Internals.Catalog.Elements["/OpenAction"] = 
        PdfInternals.GetReference(dict);
    document.Save(Server.MapPath("2.pdf"));
    frame1.Attributes["src"] = "2.pdf"; 
    

    【讨论】:

      【解决方案2】:

      最后我做到了,但我必须使用 IFRAME,我在 aspx 中定义了一个 IFrame 并没有设置 src 属性,在我制作的 cs 文件中生成了 pdf 文件并设置了 iFrame 的 src 属性作为生成的pdf文件名,像这样;

      Document pdf = new Document(PageSize.LETTER);
      PdfWriter writer = PdfWriter.GetInstance(pdf, 
      new FileStream(Request.PhysicalApplicationPath + "~1.pdf", FileMode.Create));
      pdf.Open();
      
      //This action leads directly to printer dialogue
      PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer);
      writer.AddJavaScript(jAction);
      
      pdf.Add(new Paragraph("My first PDF on line"));
      pdf.Close();
      
      //Open the pdf in the frame
      frame1.Attributes["src"] = "~1.pdf";
      

      这就是诀窍,但是,我认为我应该实施您的解决方案 Stefan,问题是我是 asp.net 和 javascript 的新手,如果我没有完整的源代码,我无法编写代码你的建议,但至少是第一步,我很惊讶我需要学习多少 html 和 javascript 代码。谢谢。

      【讨论】:

        【解决方案3】:

        您可以在 pdf 中嵌入 javascript,以便用户在浏览器加载 pdf 后立即获得打印对话框。

        我不确定 iTextSharp,但我使用的 javascript 是

        var pp = this.getPrintParams();
        pp.interactive = pp.constants.interactionLevel.automatic;
        this.print(pp);
        

        对于 iTextSharp,请查看http://itextsharp.sourceforge.net/examples/Chap1106.cs

        【讨论】:

          【解决方案4】:

          另外,试试这个宝石:

          <link ref="mypdf" media="print" href="mypdf.pdf">
          

          我还没有测试过它,但是我读过它,它可以用这种方式来打印 mypdf.pdf 而不是页面内容,无论你用什么方法来打印页面。

          搜索 media="print" 以查看更多信息。

          【讨论】:

          • 我发现这种方法在 IE8 上打印了一个空白页。
          • 好主意,但我无法让它工作。 Chrome 和 FF 只是忽略它,IE9 给出一个空白页面。
          【解决方案5】:

          pdf 是嵌入在带有 embedd-tag 的页面中还是只是在框架中打开或者您如何显示它?

          如果是嵌入的,只要确保对象被选中,然后执行 print()。

          获取嵌入文档的引用。

          var x = document.getElementById("mypdfembeddobject");  
          x.click();
          x.setActive();
          x.focus();
          x.print();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-12
            • 2018-06-29
            相关资源
            最近更新 更多