Silent pdf printing without applet

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

HttpContext.Current.Response.ContentType = "application/pdf"; HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=recibo.pdf"); HttpContext.Current.Response.BinaryWrite(ms.ToArray()); 

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" "">    
<html xmlns="" >   
<head runat="server">  
<script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>  
<script src="js/default.js" type="text/javascript">
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" onclick="printPDFSilently();" />
<div id="diviframe"></div>

Here the jQuery:

function printPDFSilently() {
 /*inserts an iframe which downloads the pdf*/
    var jIframe = jQuery(&quot;<a href="/PdfPrinter.aspx">/PdfPrinter.aspx</a>&quot;);

Hope this helps to anyone.


One comment

  1. i wrote an article how to open and show and silent print pdfs in Windows and Mac OSX with examples (using native commands):


    even if is for Adobe Air you can you native commands examples for any other programming language

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: