Click here to Skip to main content
15,867,986 members
Articles / Web Development / ASP.NET

Printing a PDF from a web page

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
15 May 2011CPOL2 min read 58.9K   20   2
How to print a PDF document but not allow the user to manipulate the PDF in any other way.

The requirement in this case is to print a PDF document, but not allow the user to manipulate the PDF in any other way. The user should see the Print Dialog box and be able to manipulate the settings. This requirement was born out of an auditing policy where the document is audited differently for printing or viewing the PDF. I found lots of web discussion about printing a pop-up window from the parent window, or trying to print the contents of an IFrame, but none of it worked for me. I was hit with security errors from IE about cross-domain issues, even though my pages were in the same domain, or just got a blank page. So to get a working solution, I had to step out of the web page.

The tool that really allowed me to do this is iTextSharp, a C# implementation of the iText Open Source Java library for manipulating PDF files, and the fact that you can use JavaScript within a PDF file. I used an ASPX page which manipulates and streams the PDF file into a zero-height, zero-width IFrame to keep the user from manipulating the PDF in any way (I realize tech-savvy users could still get the PDF). I embedded the JavaScript into the PDF, where it runs once the document is loaded and pops up the Print dialog from Adobe Reader (not the browser).

The JavaScript for printing a PDF is not so complex, I embedded it into a function:

C#
protected string GetAutoPrintJs()
{
    var script = new StringBuilder();
    script.Append("var pp = getPrintParams();");
    script.Append("pp.interactive= pp.constants.interactionLevel.full;");
    script.Append("print(pp);"); return script.ToString();
}

In the code-behind for the ASPX page that will stream the PDF and place the script inside the PDF file, I built this function:

C#
protected void StreamPdf(Stream pdfSource)
{
    var outputStream = new MemoryStream();
    var pdfReader = new PdfReader(pdfSource);
    var pdfStamper = new PdfStamper(pdfReader, outputStream);
    //Add the auto-print javascript
    var writer = pdfStamper.Writer;
    writer.AddJavaScript(GetAutoPrintJs());
    pdfStamper.Close();
    var content = outputStream.ToArray();
    outputStream.Close();
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(content);
    Response.End();
    outputStream.Close();
    outputStream.Dispose();
}

The PDFReader and PDFStamper classes are members of the iTextSharp library. I used the classes together to get the stream for output and also access the PDFWriter object that allowed me to add the JavaScript.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Hewlett-Packard
United States United States
Dave has been programming for a living since 1995, working previously with Microsoft technologies modeling internal business processes, and now working as a mobile architect and team lead. He is currently employed by DXC.technology in the metropolitan Detroit area.

Comments and Discussions

 
QuestionCan you send my a source code of this article on my e-mail? Pin
Chonov19-Mar-14 1:07
Chonov19-Mar-14 1:07 
Generalnot working after upload Pin
Yogeshwari Jadhav8-Aug-12 2:09
Yogeshwari Jadhav8-Aug-12 2:09 
I tried above code, it is working fine on localhost, but not working after upload.
I not able to trace why it is not working.
Can you please help me out?

Thanks,

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.