Published: 03 Oct 2007
By: Deepak Raghavan
Download Sample Code

This article shows how to watermark PDFs using the open source PDF generation library iTextSharp, C#, and ASP.NET.

Introduction

Often enough, Web Applications use PDF documents which may be printable versions of pages, custom forms, manuals etc. When the document consists of critical or sensitive information, the pages are watermarked with the company logo or some other custom image file. There are some off-the-shelf commercial products which have this feature. This article looks at such an implementation using the open source PDF generation library iTextSharp, C#, and ASP.NET.

Using Http Handlers

When a client sends an HTTP request to the browser, the IIS on the WebServer gets to see it first. If the requested resource has an .aspx, .asmx, or .ashx extension, the request is routed to the .NET engine as these files are registered with IIS to be handled by the ASP.NET runtime engine. At this point, the request passes through the ASP.NET HTTP pipeline. The first step is to process the request through an array of Http Modules such as Caching, Authentication, Authorization, SesssionState; before associating it with a HttpHandler endpoint. The .NET runtime processes the request through multiple modules and sends it to an HttpHandler factory. The factory chooses an appropriate handler depending on the file extension, which is used to create the output response. The response can be HTML output in the case of an .aspx request, SOAP response in the case of an .asmx request and custom handling of the request in the case of a .ashx request.

When a PDF file is requested through a browser, IIS is responsible to handle the request and, for example, invokes the Adobe Acrobat Reader to launch the requested file. In the current article, we want to intercept the PDF file requested, add a watermark to it and render it over the browser. If such a custom behavior is desired, we can perform the following steps:

  • Register .pdf file types within the web application with .NET runtime
  • Create a custom handler inheriting IHttpHandler to process the .pdf requests
  • Let the web.config file know that we have a custom handler in place for .pdf files.

iTextSharp

iTextSharp is an open source .NET port of the iText library used for Java Applications. The library can be used to automate PDF creation and/or manipulation. In the current article, the iTextSharp API is used within the custom Http Handler to intercept the PDF request, read the PDF stream, add an image to it and stream it back to the browser.

Pdf Handler

All custom handlers should implement the IHttpHandler interface shown below.

The IHttpHandler interface has two methods to be implemented.

  • IsResuable indicates whether the IHttpHandler instance is reusable
  • ProcessRequest has the main implementation for the associated request type

To create a custom handler, a class library is created within Visual Studio 2005 and a class (PdfHandler) is added to it which implements IHttpHandler. The source code for PdfHandler implementation is shown below

Listing 2: PdfHandler Implementing IHttpHandler

The classes used in the ProcessRequest method and their brief descriptions are listed in the table below:

Table 1: Classes within iTextSharp API and their descriptions

Class NameUsage Description
PdfReaderReads and parses a PDF document at the provided Url string
PdfStamperUsed to add new content to multiple pages of a document. The content is an instance of PdfContentByte
PdfContentByteObject containing user positioned text and/or graphics on a PDF page.

In the previous listing, the IsReusable property always returns true. This means that a single PdfHandler instance can be used to process multiple concurrent requests (from different clients) served on multiple worker threads. This is possible because we don't have common shared resources or data across multiple requests.

Test Web Application

The following steps are followed to create the demo web application:

  • Create a new web site in your choice of directory and name it TestPdfRender.
  • Add the binary output of the custom handler as a reference
  • Add the image used for watermarking purposes. We have used the sample image provided on the iTextSharp website, but this can be replaced by any image of choice.
  • Modify the web.config file with the following code. The path is set to a .pdf file and verb="*" means that the handler is used across GET, POST, HEAD. TRACE, and DEBUG operations.
  • Create a virtual directory within IIS called TestPdfRender and point it to the website.
  • In the project properties of TestPdfRenderer, within Start Options, set the Server section to "Use custom server" and set the Base Url to the location of this test web site within IIS (In our case, it is http://localhost/TestPdfRender).
  • To map the .pdf extension through IIS: within IIS, right click on TestPdfRender->properties->under Virtual Directory tab, click on configuration->add a new extension with extension .pdf and set Executable to the .NET runtime ("c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll"), keep the rest of the sections to their default values. Click on OK to close all the open configuration windows.

Demo

The application is ready to be tested. The sample code files included use a test PDF document and an image file which can be replaced. Open "Default.aspx" and add this test hyperlink control:

Now, set Default.aspx to be the start page. When the link is clicked the demo file shows the image watermarked in the center. The actual position of the image can be altered by changing the coordinates in listing 1, within the ProcessRequest method:

Summary

In this article we have seen how to implement a custom HttpHandler to watermark PDF Documents. The meat of the implementation is within the ProcessRequest method with the PdfHandler. This handler can be used across multiple web applications if the handler is registered through IIS and referenced within the web.config file.

References

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Deepak Raghavan

Deepak has been architecting and developing enterprise applications for the past 14 years and currently works as an Architect in Minneapolis. His interests lie in the upcoming Microsoft technologies, SOA, SharePoint 2010, Agile and XP to name a few. He is on the executive board for the MN SharePoint...

This author has published 3 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
jQuery Mobile ListView
In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
Exception Handling and .Net (A practical approach)
Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
JQuery Mobile Widgets Overview
An overview of widgets in jQuery Mobile.
Book Review: SignalR: Real-time Application Development
A book review of SignalR by Simone.

You might also be interested in the following related blog posts


Export Word to PDF docs compliant to PDF/A-1b via Aspose.Words read more
Using jQuery in SharePoint to Display Notifications for Open Tasks read more
PDF Linearization (Optimization), Faster display of PDF documents read more
Aspose.Total for .NET Q4 2008 Released read more
First Look: RadGrid 5 PDF Export read more
Allow Your End Users To Generate PDF Documents read more
Opening a PDF in a Popup Window In Internet Explorer read more
Aspose.Pdf.Kit for .NET - 2.3.0.0 Released read more
Aspose.Pdf for .NET - 3.3.0.0 Released read more
Introduce PDF Export Kit read more
Top
 
 
 

Discussion


Subject Author Date
placeholder SharePoint Implementation Erik Dobrauer 10/9/2007 10:32 AM
RE: SharePoint Implementation Deepak Raghavan 10/9/2007 10:55 AM
placeholder RE: RE: SharePoint Implementation Erik Dobrauer 10/9/2007 12:04 PM
RE: RE: RE: SharePoint Implementation Steve Robert 6/30/2009 9:40 AM
placeholder save some memory Keith o 10/17/2007 8:58 AM
RE: save some memory Deepak Raghavan 10/17/2007 3:40 PM
placeholder Thanks a lot Martin Gold 9/9/2008 7:41 AM

Please login to rate or to leave a comment.