Thursday, January 29, 2015

Export System.Data.DataTable to PDF using AT.PDFReportCreator

I was engaged in a big web-application which has lots of inquiry pages. As we all know, each inquiry page has filtering options and a grid which displays the data. With each inquiry page we need to show export buttons, in PDF and Excel. Exporting data in excel was simple, we just need to create a sheet and put all the data in there. But export of PDF was difficult, client was asking to have a good formatted PDF file, with company logo, total number of pages, heading and with inquiry parameters.


  • Provide your own Logo and Watermark files, yes AT logo and watermark is NOT hard-coded you can change it :-)
  • Page X of Y in the footer
  • Date and Time of printing in the footer
  • Custome Report Heading
  • Provide Page Layout, for example custom page width, height, margins and orientation
  • Define which columns should be included in PDF from DataTable, in-case if you don't want all columns to be printed
  • Define widths for each column
  • Provide Parameters which will be displayed in the report header

iTextSharp is a very powerful library for creating PDF files in .net Applications. So I decided to create a generic API using iTextSharp library, which can be used to create PDF for every inquiry screen. We just need to pass the Data to the utility and it will create the PDF File. Usually I don't have time, so I did a quick work and created a DLL, which can be used with .net Applications, like or Desktop. Also I tried my best to keep the usage as simple as possible.

Below is the Export PDF code written for a Web Application using my wrapper library around iTextSharp.

protected void btnCreatePDF_Click(object sender, EventArgs e)
        using (System.Data.DataTable dt = this.GetData())
            var reportParam = new AT.PDFReportCreator.PDFReportParam()
                ReportData = dt

            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Customers.pdf");
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/pdf";

            AT.PDFReportCreator.PDFReportCreator.CreatePDF(HttpContext.Current.Response.OutputStream, reportParam);

    catch (Exception ex)
        Trace.Write("Export PDF", ex.Message, ex);

Due to time limit, I only came up with DataTable export, while we have other possibilities as well, such as,

  • Export GridView Control to PDF
  • Export Entity Framework's List of Entity to PDF
  • Export Desktop Application's DataGridView Control to PDF
For above scenarios we can convert them into DataTable, for example iterate each GridView control row and add the content in DataTable and then pass that DataTable to Export utility.

Downloads: Download this file, it contains a Web Application project which shows the export option. Also contains the AT.PDFReportCreator.dll and iTextSharp.dll files. If you want to quickly start testing and don't want to go in complex details, then just download this zip file, extract the DLLs and use in your project. Source code of export API, which is actually a wrapper around iTextSharp

No comments: