In one of the previous articles we demonstrated how to export a GridView to excel. In this article we will demonstrate how to export GridView to PDF format using the ITextSharp library.
Introduction:
In one of the previous articles we demonstrated how to export a GridView to excel. In this article we will demonstrate how to export GridView to PDF format using the ITextSharp library.
Creating the Entity Class:
Let's create a simple entity class "Customer" and populate it with some dummy data. The Customer class looks like the following:
Now, let's create a GridView control and poppulate it with the dummy Customer objects.
The above code will populate the GridView with dummy Customers. Take a look at the screenshot of the GridView below:
Now, let's try to export this GridView to PDF.
How *NOT* to Export a GridView to PDF:
Here is the code which is used to export GridView to PDF.
When you export the GridView to PDF using the above code it will seem that the export is taking place correctly but when you try to open the file you will realize that the file is corrupted. Although this particular code works when exporting to Excel or Text files this code will fail when exporting to PDF files. To export GridView to PDF we will need to use a third party library ITextSharp. Let's see how we can use this library.
Downloading the ITextSharp Library:
You can download the ITextSharp library using the following link:
The library is also included in the download sample files.
Exporting the GridView to Excel Using ITextSharp and ResponseOutputStream:
We are going to use the ITextSharp library to write the document to the ResponeOutputStream. Let's take a look at the code:
The above code is using the Document object which represents a Pdf document. The GridView is rendered into the HtmlTextWriter. The HtmlTextWriter uses the support of StringWriter and StringBuilder to get the HTML for the GridView control. The PdrWriter.getInstance is used to set the Document instance to the Stream which in this case is the Response Stream. The HtmlParser class is responsible for parsing the rendered GridView HTML. This is responsible for creating a table display for the PDF document. If we omit the use of HtmlParser then the raw HTML will be displayed in the Pdf document.
Take a look at the screenshot below which shows the GridView as a Pdf Document:
As, you can see the Pdf document is poorly styled and the table cells are of variable length. Let's see how we can improve the Table cell rendering.
Creating Pdf Document Using ITextSharp Tables:
The ITextSharp library includes the Table class which is used to create Tables in PdfDocuments. We will use the Table class to improve our display.
The button click will fire the following event:
The ExportToPDFCollection method is responsible for creating the Table using the Customer collection. It takes a single parameter which is the names of the columns we will want to export to the Pdf document.
Here is the implementation of the ExportToPDFCollection method:
The above code creates the Table and append cells to the Table. Each cell is populated with the particular propertyname of the Customer class. The GetPropertyValue method uses reflection to get the value from the property.
Here is the result:
Although the resultant Pdf document is not pretty but you can always make adjustments to make it better. Also note that the cells are property listed.
Conclusion:
In this article we learned how to export GridView to Pdf. We discussed different ways of performing this operation. In the future articles we will try to demonstrate how to customize the display of the exported Pdf document.