PDFs Using iTextSharp

iStock_000002747386Medium There are several libraries on the market now that allow you to create PDF documents from your .NET applications.  The one I’ve chosen to use is extSharp, an open source library that is a port of the open source library for Java,  iText.

While there are several sites on the Internet that provide examples of how to use iText, I’ve found that the documentation for iTextSharp is a little harder to come by.  So I thought it might be helpful if I provided some posts on how I use iTextSharp along with some of the gotchas I’ve encountered along the way.


To use iTextSharp, you will need to add a reference to the library in your code, or simply drop the code into your bin directory of your ASP.NET application.

The main trick in translating the iText samples and documentation to iTextSharp is that iTextSharp makes the Java properties in iText (getPropertyName()/setPropertyName()) .NET properties, (PropertyName).

You’ll also need to know that the namespace for iTextSharp is different from iText.  Be sure to include the following line at the top of any code you write that uses iTextSharp

using iTextSharp.text;
using iTextSharp.text.pdf;

To get your ASP.NET page to return a PDF file, you’ll want to add the following code at the top of your Page_Load event handler:

Response.ContentType = "application/pdf";
Response.AddHeader("ContentType", "application/pdf");

The Response.Clear() line clears out any input that has already been sent back to the browser.

The next line tells the browser that what is coming back is a PDF, not HTML.

The “Content-Disposition” line allows the browser to know that we want the file IN the browser window rather than downloading the file.

Finally, yes, I know there are two ContentType lines.  But I took this from working code and while I can’t remember why I wrote it like that, it works.  If you can get it working with only one line, go for it.

Now that we have the basics out of the way, we can concentrate on actually generating PDFs in future posts.

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • iText IN ACTION – Creating and Manipulating PDFiText IN ACTION – Creating and Manipulating PDF While this isn’t specifically targeted at iTextSharp, which we’ve been covering in recent posts, this is really the closest book you are going to find on the subject. The basics are […]
  • iTextSharp – HTML to PDF – PrerequisitesiTextSharp – HTML to PDF – Prerequisites Before we get into the nitty gritty of parsing the HTML so that we can create PDF code from it, it is important that we develop the concept of how text layout works in iTextSharp.  […]
  • iTextSharp – HTML to PDF – Parsing HTMLiTextSharp – HTML to PDF – Parsing HTML Now that we have the HTML cleaned up, the next thing we will want to do is to parse the HTML. In my actual code for this, I parse the HTML and create the PDF at the same time, but for […]
  • iTextSharp – HTML to PDF – Positioning TextiTextSharp – HTML to PDF – Positioning Text The next series of things I’m going to introduce about using iTextSharp are all going to lead toward taking HTML text and placing it on the PDF document. There are several items we […]
  • iTextSharp – Adding ImagesiTextSharp – Adding Images Last week I showed how to use form fields to control placement of dynamic data. But what if you want to dynamically place images in your PDF?  You can stuff them into a form field […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback

    25 June 2009 at 7:06am
    [...] already covered setting the MIME type information in our first post, so the rest of ...
  • iTextSharp – The easy way
  • Josh Blair


    Thanks for the post. I wanted to ask if you have ever had the requirement to print PDF documents to networked printers from a web app (Intranet app specifically).

    I have accomplished this with a ‘not-so-elegant’ solution…but it works.

    From a PocketPC, a user opens an ASP.NET screen that I built, they barcode scan an MO number, then I generate SSRS reports based on input parameters, save these reports as PDF to disk, then I make a WCF call to a simple Windows Service that I build that acts as a WCF host. This service receives the WCF request from the web app and prints the PDF document to the correct network printer by shelling out to Adobe. I know, kind of a ‘Rube Goldberg’ rig but it works.

    Can you think of a more elegant solution for printing PDF documents to a networked printer? That doesn’t cost much?



    • Dave

      Sounds about as good as your going to get.

  • Pingback: iTextSharp – The easy way