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.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("ContentType", "application/pdf"); Response.AddHeader("Content-Disposition", "inline;filename=\"FileName.pdf\"");
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.
Other post in iTextSharp
- PDFs Using iTextSharp - June 17th, 2009
- iTextSharp – The easy way - June 24th, 2009
- iTextSharp – Adding Images - June 30th, 2009
- iTextSharp – HTML to PDF – Positioning Text - July 8th, 2009
- iTextSharp – HTML to PDF - Prerequisites - July 14th, 2009
- iTextSharp – HTML to PDF – Cleaning HTML - July 20th, 2009
- iText IN ACTION – Creating and Manipulating PDF - July 24th, 2009
- iTextSharp – HTML to PDF – Parsing HTML - July 28th, 2009
- iTextSharp – HTML to PDF – Writing the PDF - August 4th, 2009
- iTextSharp – HTML to PDF – Finishing Up - August 12th, 2009
- iTextSharp Tables - September 17th, 2009