Nelson Miranda
Nelson Miranda

Reputation: 5554

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.

Upvotes: 16

Views: 77182

Answers (5)

frenchone
frenchone

Reputation: 1597

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

Upvotes: 1

Nelson Miranda
Nelson Miranda

Reputation: 5554

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.

Upvotes: 6

Tim
Tim

Reputation:

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

Upvotes: 0

Stefan
Stefan

Reputation: 11509

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.

Upvotes: 0

Stefan
Stefan

Reputation: 11509

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();

Upvotes: 1

Related Questions