I my current project I have to print a pdf without using an applet (all because the spanish electronic identity card ask for its password any time it open a connection). We have to do it silently, or a least with the less posible user intervention.
The idea is to insert an iframe which calls for the pdf, inside it we set javascript into the pdf for printing. I didn’t know that I could insert js into a pdf, but it is possible and easy with itext/itextsharplibrary.
Here is the page_load event in the page that returns the pdf. This is where all the magic is done:
protected void Page_Load(object sender, EventArgs e) { MemoryStream ms = new MemoryStream(); var urlPdf = new Uri(getBaseUrl() + "prueba.pdf"); PdfReader ps = new PdfReader(urlPdf); /*inserts js into pdf*/ PdfStamper pdf = new PdfStamper(ps, ms); pdf.JavaScript="this.print(false);"; pdf.Close(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=recibo.pdf"); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); HttpContext.Current.Response.End(); }
And later we have just a page in which the iframe is inserted using jQuery:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SilentPDFPrinting._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title> </title> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="js/default.js" type="text/javascript"> </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="button" onclick="printPDFSilently();" /> </div> <div id="diviframe"></div> </form> </body> </html>
Here the jQuery:
function printPDFSilently() { /*inserts an iframe which downloads the pdf*/ var jIframe = jQuery("<a href="/PdfPrinter.aspx">/PdfPrinter.aspx</a>"); jIframe.insertAfter("#diviframe"); }
Hope this helps to anyone.
i wrote an article how to open and show and silent print pdfs in Windows and Mac OSX with examples (using native commands):
http://www.onyrix.com/2012/04/adobe-air-pdf-silent-print-with-acrobat-reader/
bye
d
even if is for Adobe Air you can you native commands examples for any other programming language