Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Export ASP.Net GridView to PDF with Custom Columns Widths using iTextSharp
The html and codebehind would become like this
Issue
After going through the iTextSharp tutorial.I found that we can create Tables with ITextSharp library and specify the column width and height as per our requirements. Thus we can create an ITextSharp table with same number of rows and columns as our GridView and specify the column widths for that ITextSharp table. This ensures that the custom column widths that we specify are not ignored in the generated PDF.
Solution
After going through the iTextSharp tutorial. I found that we can create Tables with ITextSharp library and specify the column width and height as per our requirements. Thus we can create an ITextSharp table with same number of rows and columns as our GridView and specify the column widths for that ITextSharp table. This ensures that the custom column widths that we specify are not ignored in the generated PDF.
Namespaces
We would require the following namespaces in order to run this project
C#
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;
Front End Design
In the front end I have placed an ASP.Net GridView Control, a button for exporting and a RadioButtonList to allow the user to select whether he wants the current page to be exported to PDF or all the pages
<asp:GridView ID="GridView1" runat="server"
        AutoGenerateColumns = "false" Font-Names = "Arial"
        Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
        HeaderStyle-BackColor = "green" AllowPaging ="true"
        OnPageIndexChanging = "OnPaging" >
        <Columns>
                <asp:BoundField ItemStyle-Width = "200px"
                        DataField = "CustomerID" HeaderText = "CustomerID" />
                <asp:BoundField ItemStyle-Width = "100px"
                        DataField = "City" HeaderText = "City"/>
                <asp:BoundField ItemStyle-Width = "50px"
                        DataField = "Country" HeaderText = "Country"/>
        </Columns>
</asp:GridView>
Binding the GridView
For this article we are using the NorthWind Database which can be downloaded from here.And we are binding the GridView using the following code snippet in the page load event. You will notice we have set different widths to the GridView columns.
C#
protected void Page_Load(object sender,EventArgs e)
{
   String strConnString = ConfigurationManager
             .ConnectionStrings["conString"].ConnectionString;
   SqlConnection con = new SqlConnection(strConnString);
   SqlCommand cmd = new SqlCommand();
   SqlDataAdapter sda = new SqlDataAdapter();
   DataSet ds = new DataSet();
   cmd.CommandType = CommandType.Text;
   cmd.CommandText = "select CustomerID,City,Country from customers";
   cmd.Connection = con;
   sda.SelectCommand = cmd;
   try
   {
      con.Open();
      sda.Fill(ds);
      GridView1.EmptyDataText = "No Records Found";
      GridView1.DataSource = ds;
      GridView1.DataBind();
   }
   catch (Exception ex)
   {
      throw ex;
   }
   finally
   {
      con.Close();
      con.Dispose();
   }
}
Now we will have to write the code on the click event of the Export Button as shown below
C#
protected void btnExportPDF_Click(object sender, EventArgs e)
{
   GridView1.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);
   GridView1.DataBind();
   //Create a table
   iTextSharp.text.Table table = new iTextSharp.text
                   .Table(GridView1.Columns.Count);
   table.Cellpadding = 5;
   //Set the column widths
   int [] widths = new int[GridView1.Columns.Count];
   for (int x = 0; x < GridView1.Columns.Count; x++)
   {
      widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value ;
      string   cellText=Server.HtmlDecode(GridView1.HeaderRow.Cells
                               [x].Text);
      iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
      cell.BackgroundColor = new Color (System
                              .Drawing.ColorTranslator.FromHtml("#008000"));
      table.AddCell(cell);
   }
   table.SetWidths(widths);
   //Transfer rows from GridView to table
   for (int i = 0; i < GridView1.Rows.Count; i++)
   {
      if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
      {
         for (int j = 0; j < GridView1.Columns.Count; j++)
         {
            string cellText = Server.HtmlDecode
                                 (GridView1.Rows[i].Cells[j].Text);
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
            //Set Color of Alternating row
            if (i % 2 != 0)
            {
               cell.BackgroundColor = new Color(System.Drawing
                                               .ColorTranslator.FromHtml("#C2D69B"));
            }
            table.AddCell(cell);
         }
      }
   }
   //Create the PDF Document
   Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
   PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
   pdfDoc.Open();
   pdfDoc.Add(table);
   pdfDoc.Close(); 
   Response.ContentType = "application/pdf";
   Response.AddHeader("content-disposition", "attachment;" +
                                   "filename=GridViewExport.pdf");    Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.Write(pdfDoc);
   Response.End();
}
As you will notice above we are looping through the GridView and creating cells in the iTextSharp table using the values from the GridView cells. Finally I am adding the table to the PDF document object which is then exported through the Response Stream. The figure below displays the exported PDF document with the custom column widths same as that specified in the GridView Control.Also you will need to add this event in order to avoid the following error.Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
C#
public override void VerifyRenderingInServerForm(Control control)
{
   /* Verifies that the control is rendered */
}
Job or extra money for students

Search Engine Rank of your blog or websites