Print PDF from ASP.Net without preview

前端 未结 5 1279
伪装坚强ぢ
伪装坚强ぢ 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"; 
    
    0 讨论(0)
  • 2020-12-08 01:58

    Is the pdf embedded in the page with embedd-tag or just opened in a frame or how are you showing it?

    If its embedded, just make sure that the object is selected and then do a print().

    Get the ref to the embedded document.

    var x = document.getElementById("mypdfembeddobject");  
    x.click();
    x.setActive();
    x.focus();
    x.print();
    
    0 讨论(0)
  • 2020-12-08 02:06

    ALso, try this gem:

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

    I havent tested it, but what I have read about it, it can be used in this way to let the mypdf.pdf be printed instead of page content whatever method you are using to print the page.

    Search for media="print" to check out more.

    0 讨论(0)
  • You can embed javascript in the pdf, so that the user gets a print dialog as soon as their browser loads the pdf.

    I'm not sure about iTextSharp, but the javascript that I use is

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

    For iTextSharp, check out http://itextsharp.sourceforge.net/examples/Chap1106.cs

    0 讨论(0)
  • 2020-12-08 02:08

    Finally I made it, but I had to use an IFRAME, I defined an IFrame in the aspx and didn't set the src property, in the cs file I made generated the pdf file and set the src property of the iFrame as the generated pdf file name, like this;

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

    And that made the trick, however, I think that i should implement your solution Stefan, the problem is that I'm new to asp.net and javascript and if I don't have a complete source code I could not code your suggestion but at least is the first step, I was very surprised how much code in html and javascript i need to learn. Thnx.

    0 讨论(0)
提交回复
热议问题