Export data displayed on grid view control to pdf using iTextsharp

Introduction

In this example we are exporting Gridview data populated with Sql Server Database to Pdf using iTextSharp.

This is the html source of the page in which i've created a gridview and a Export Button to export Grid View data to Pdf file.

gridviewcontrol.gif

The complete code of Register.aspx.cs Page in Code Behind

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

public partial class Register : System.Web.UI.Page
{
    BAL objBAL = new BAL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillgrid();
        }  
    }
    public void fillgrid()
    {
        DataSet ds = new DataSet();
        ds = objBAL.Fill_Grid();//Function defined in BAL Class
        grdDetail.DataSource = ds;
        grdDetail.DataBind();
        
   }
   protected void btnExport_Click(object sender, EventArgs e)
   {
        const string attachment = "attachment; filename= grdDetail.pdf";
 
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/pdf";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ClearContent();
 
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grdDetail.RenderControl(htw);
       
        StringReader
sr = new StringReader(sw.ToString());
 
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
 
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
 
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
 
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
 
        Response.Write(pdfDoc);       
        Response.End(); 
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}

App_Code BAL.cs

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data;

public class BAL
{
     public BAL()
     {
          //
          // TODO: Add constructor logic here
          //
     }
 
     #region "variable"
                
     #region "Properties"
     #region "Functions"

    public DataSet Fill_Grid()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT * from RegisterInfo", conn);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter adap = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        return ds;
    }
   #endregion
}

Note: If in case code works well but pdf still do not generated. Please put EnableEventValidation="false" in source code of Register.aspx.

Next Recommended Reading Add image in PDF using iTextSharp