Generate PDF Formatted Report From DataTable

Monday, July 9, 2012
Here I am using Itextsharp.dll, please download and add reference to the project.And then --

This is the class file ::

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;

/// <summary>
///  PDFExporter:  Created by Abhirup Sinha Roy,help to generate Customise PDF (used for report purpose)
/// </summary>
public class PDFExporter
{
    private readonly DataTable dataTable;
    private readonly string fileName;
    private readonly bool timeStamp;

    public PDFExporter(DataTable dataTable, string fileName, bool timeStamp)
    {
        this.dataTable = dataTable;
        this.fileName = timeStamp ? String.Format("{0}-{1}", fileName, GetTimeStamp(DateTime.Now)) : fileName;
        this.timeStamp = timeStamp;
    }

    public void ExportPDF()
    {
        HttpResponse Response = HttpContext.Current.Response;
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
       
        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 10, 10, 90, 10);

        // step 2: we create a writer that listens to the document
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

        //set some header stuff
        document.AddTitle(fileName);
        document.AddSubject(String.Format("Table of {0}", fileName));
        document.AddCreator("www.i-novaonline.com");
        document.AddAuthor("KLJ");

        // step 3: we open the document
        document.Open();

        // step 4: we add content to the document
        CreatePages(document);

        // step 5: we close the document
        document.Close();    
    }

    public void ExportPDF1()
    {
        HttpResponse Response = HttpContext.Current.Response;
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");

        // step 1: creation of a document-object
        Document document = new Document(PageSize.A4, 10f, 10f, 90f, 10f);

        // step 2: we create a writer that listens to the document
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

        //set some header stuff
        document.AddTitle(fileName);
        document.AddSubject(String.Format("Table of {0}", fileName));
        document.AddCreator("www.i-novaonline.com");
        document.AddAuthor("KLJ");

        // step 3: we open the document
        document.Open();

        // step 4: we add content to the document
        CreatePages(document);

        // step 5: we close the document
        document.Close();

    }

    private void CreatePages(Document document)
    {
        document.NewPage();
        document.Add(FormatPageHeaderPhrase(dataTable.TableName));
        PdfPTable pdfTable = new PdfPTable(dataTable.Columns.Count);
        pdfTable.DefaultCell.Padding = 3;
        pdfTable.WidthPercentage = 100; // percentage
        pdfTable.DefaultCell.BorderWidth = 2;
        pdfTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

        //PdfPCell cell = new PdfPCell(new Phrase(": List of Certificate For  Courier :"));
        //cell.Colspan = 9;
        //cell.Border = 1;
        ////cell.Height = 9f;
        //cell.HorizontalAlignment = 1;
        //cell.BackgroundColor = iTextSharp.text.BaseColor.LIGHT_GRAY;
        ////c = iTextSharp.text.BaseColor.BLUE;
        //pdfTable.AddCell(cell);

        foreach (DataColumn column in dataTable.Columns)
        {
            pdfTable.AddCell(FormatHeaderPhrase(column.ColumnName));
        }
        // this is the end of the table header
        pdfTable.HeaderRows = 1;
        pdfTable.DefaultCell.BorderWidth = 1;

        foreach (DataRow row in dataTable.Rows)
        {
            foreach (object cells in row.ItemArray)
            {
                //attach the formated output( all cells of datatable )
                pdfTable.AddCell(FormatPhrase(cells.ToString()));
            }
        }
        document.Add(pdfTable);
    }
    private static Phrase FormatPageHeaderPhrase(string value)
    {
        return new Phrase(value, FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD, new BaseColor(102, 0, 153)));
    }
    private static Phrase FormatHeaderPhrase(string value)
    {
        return new Phrase(value, FontFactory.GetFont(FontFactory.COURIER, 8, Font.ITALIC, new BaseColor(0, 0, 255)));
    }
    private Phrase FormatPhrase(string value)
    {
        return new Phrase(value, FontFactory.GetFont(FontFactory.COURIER, 6));
    }
    private string GetTimeStamp(DateTime value)
    {
        return value.ToString("yyyyMMddHHmmssffff");
    }
}


You have to call that class from an event like button click as ::


PDFExporter pdf = new PDFExporter(getPrintData(), "customer", true);
 pdf.ExportPDF();