Bypassing Print Dialog in Browser

I’m sorry I haven’t written in a few weeks. My wife and I were just blessed with a newborn and all though an awesome experience and unlike anything else, it is very tiring and trying to say the least. Smile.

In the project I am currently working on, there is a requirement to have a printing option. Usually this would be a relatively easy task, but the requirement also stated that you had to control the amount of copies of that particular PDF document. The reason for this is that the PDF document was not allowed to be duplicated once printed. My first thought was to either use Silverlight or Active X controls. Silverlight apparently (in browser mode) does not have a way around the print dialog because it cannot directly access the operating system. For unknown reasons to me, the client is completely set against using Active X even though this is an internal application, so that was also out.

The only way that we came up with to accomplish this task is to use the Adobe Reader plugin to bypass the dialog. To do this, there is a property within the Adobe plugin called interactionLevel that you can set to force the plugin to print silently. Luckily the plugins API that interfaces with JavaScript has this property settable.

The JavaScript used looks like this below:

var res = app.setTimeOut('var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.automatic;this.print(pp);', 200);

You will notice that the property being set and an actual print is set inside a JavaScript setTimeOut. This is to force the print to occur 200 milliseconds after the DOM and PDF loads. This makes the print process appear seemless and automatic. The PDF creator I used was called ITextSharp. I found it extremely easy to work with and with the help of this site, I was able to use their AddJavascript method to keep all the code in one place.

Here is an example of it’s usage:

 using (var document = new Document(new Rectangle(300f, 200f)))

                    var writer = PdfWriter.GetInstance(document, documentStream);
                    // Open the Document for writing

                    // Insert JavaScript to print the document after a fraction of a second
                    string jsText = "var res = app.setTimeOut('var pp = this.getPrintParams();pp.interactive = pp.constants.interactionLevel.automatic;this.print(pp);', 200);";
                    PdfAction js = PdfAction.JavaScript(jsText, writer);

                    var fonts = TemplateBuilder.CreateFontSettings();

                    // Add Title
                    document.Add(new Paragraph("Header Here", fonts[0]));

I am always a fan of using the ‘using’ keyword to make sure that my open connections and streams are closed when out of scope, but there is a Close() method as well.

Next time, I hope to make a new tutorial on using ITextSharp.


Happy Coding Smile!!


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET 4.5, ASP.NET, C#, Classes, MVC and tagged , , , , , , , . Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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