Print PDF from ASP.Net without preview

前端 未结 5 1287
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 01:33

I\'ve generated a pdf using iTextSharp and I can preview it very well in ASP.Net but I need to send it directly to printer without a preview. I want the user to click the pr

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 01:43

    It's a little more tricky if you're using pdfsharp but quite doable

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

提交回复
热议问题