views:

14685

answers:

5

Hi, 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.

A: 

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();
Stefan
A: 

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.

Stefan
I find this approach prints a blank page on IE8.
Martin Clarke
A: 

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

+3  A: 

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.

nmiranda
A: 

How can you add a close window command to the above. By the way it works great :)

godleuf