Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

c# - Print PDF from ASP.Net without preview

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 print button and automatically the document prints.

I know that a page can be sent directly to printer using the javascript window.print() but I don't know how to make it for a PDF.

Edit: it is not embedded, I generate it like this;

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

And here I am.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...