Print PDF from ASP.Net without preview

前端 未结 5 1292
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  萌比男神i
    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.

提交回复
热议问题