Friday, December 10, 2010

Export to PDF file in ASP.Net – Gridview to PDF, ASPX Page Content to PDF

Creating data driven application is one of the most commonly done task in our day today application development. In these applications, reporting and generating reports in various formats is one of the repeated requirements we will get. Read the article Export to Excel in ASP.Net 2.0 – Gridview to Excel, DataTable to Excel which discusses some handful of techniques to export the data to excel file. Apart from excel reports, generating PDF report is one of the most common reporting requirement we will get in data driven application. Exporting data to PDF format is not a straight forward task like we do for excel file generation. To do this, we have to employ some 3rd party component or develop a component ourselves by understanding the PDF file standards.

Since, developing a component ourselves is complicated and time consuming task we can try using any free 3rd party component available in the market. In this article, we will try using one of most widely used PDF generation component called iTextSharp or iText PDF component.

According to iText PDF official site,
iText is a library that allows you to generate PDF files on the fly.
iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.

In this article, we will see about,
1.      Simple PDF generation from an in-memory string or HTML string
2.      Export Page Content to PDF
3.      Export GridView control to PDF

1.      Open Visual Studio 2008, Click File >Website and choose ASP.Net Website.
2.      Select a language of your choice. I have selected C#. You can name your website as per your need.
3.      Download the iText PDF component from the official site (or from here) and add it to your project through add reference dialog box.