Print PDF iTextSharp

I am trying to create a method that sends a PDF file directly to my printer (as a result, a print dialog will appear).

Below is the code I was working on - most of it was found on the forums here . It works fine if I use iTextSharp to create a new PDF document, but as soon as I try to insert some JavaScript into an existing file, I get an exception when calling the print() method saying

The object does not support the property or method 'print'

 <script type="text/javascript"> function load() { try { var x = document.getElementById("frame1"); x.print(); } catch (err) { } } </script> <body onload="load();"> <form id="form1" runat="server"> <div> <iframe id="frame1" src="C:/1686850_1.pdf" runat="server" frameborder="0" style="height: 0px; width: 0px;" /> </div> </form> </body> </html> 

.CS file

 using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; public partial class Print : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SetPDF(File.ReadAllBytes("C:\\1686850.pdf"), "C:\\1686850_1.pdf"); //test files } private void SetPDF(byte[] file, string outputPath) { PdfReader reader = new PdfReader(file); int pageCount = reader.NumberOfPages; Rectangle pageSize = reader.GetPageSize(1); Document pdf = new Document(pageSize); PdfWriter writer = PdfWriter.GetInstance(pdf, new FileStream(outputPath, FileMode.Create)); pdf.Open(); //This action leads directly to printer dialogue PdfAction jAction = PdfAction.JavaScript("this.print(true);\r", writer); writer.AddJavaScript(jAction); //Omitting this loop and simply adding some text to the file produces the behavior I want. for (int i = 0; i < pageCount; i++) { pdf.NewPage(); PdfImportedPage page = writer.GetImportedPage(reader, i + 1); writer.DirectContent.AddTemplate(page, 0, 0); } pdf.Close(); //Open the pdf in the frame frame1.Attributes["src"] = outputPath; } } 
+4
source share
2 answers

I found a way to do this here: http://wskidmore.com/2011/03/pdf-initial-view-settings-itextsharp/

Based on this, I wrote this code:

 private void PrintMenu() { ... var notUriPath = Server.MapPath("~/" + filePathName); var doc = new Document(); var reader = new PdfReader(notUriPath); using (var memoryStream = new MemoryStream()) { var writer = PdfWriter.GetInstance(doc, memoryStream); doc.Open(); // this action leads directly to printer dialogue var jAction = PdfAction.JavaScript("this.print(true);\r", writer); writer.AddJavaScript(jAction); var cb = writer.DirectContent; doc.AddDocListener(writer); for (var p = 1; p <= reader.NumberOfPages; p++) { doc.SetPageSize(reader.GetPageSize(p)); doc.NewPage(); var page = writer.GetImportedPage(reader, p); var rot = reader.GetPageRotation(p); if (rot == 90 || rot == 270) cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height); else cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0); } reader.Close(); doc.Close(); File.WriteAllBytes(notUriPath, memoryStream.ToArray()); } theIframeForPrint.Attributes.Add("src", fullFilePath); } 

Hope this helps!

+9
source

You want to add Javascript to PDF to open the print dialog, not the web page (unless you really need the web page print dialog, not the PDF print dialog). I have done this before, but not with iTextSharp; but a quick google search should tell you how to add Javascript using iTextSharp to open the print dialog. A.

0
source

All Articles