title: how to export grid view data to pdf using itext sharp
body: <p>i just want to export grid view data with itextSharp. here is my code how i export grid view to excel is there way to use this technique and create table with using itextSharp.</p>
  
  <p>this is my code :</p>
  
  <pre><code> protected void ibtnxls1_Click(object sender, ImageClickEventArgs e)
      {
          DataTable dt = (DataTable)Session["PostTable"];
          string fileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
          string attachment = "attachment; filename=" + fileName + ".xls";
          Response.ClearContent();
          Response.AddHeader("content-disposition", attachment);
          Response.ContentType = "applicssssation/vnd.ms-excel";
          string sTab = "";
          foreach (DataColumn dc in dt.Columns)
          {
              HttpContext.Current.Response.Write(sTab + dc.ColumnName);
              sTab = "\t";
          }
          HttpContext.Current.Response.Write("\n");
          int i;
          foreach (DataRow dr1 in dt.Rows)
          {
              sTab = "";
              for (i = 0; i &lt; dt.Columns.Count; i++)
              {
                  HttpContext.Current.Response.Write(sTab + dr1[i].ToString());
                  sTab = "\t";
              }
              HttpContext.Current.Response.Write("\n");
          }
          HttpContext.Current.Response.End();
      }
  </code></pre>
  
  <p>i include this screen shot below answer :</p>
  
  <p><img src="https://i.stack.imgur.com/Q8niR.png" alt="enter image description here"></p>
  
  <p>this solution works for me :</p>
  
  <pre><code>  DataTable dt = (DataTable)Session["PostTable"];
          iTextSharp.text.Table table = new iTextSharp.text.Table(dt.Columns.Count);
          table.Cellpadding = 2;
          table.Width = 100;
          //Transfer rows from GridView to table
          for (int i = 0; i &lt; dt.Columns.Count; i++)
          {
              string cellText = Server.HtmlDecode(dt.Columns[i].ToString());
              iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
              cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
              table.AddCell(cell);
          }
  
          for (int i = 0; i &lt; dt.Rows.Count; i++)
          {
              for (int j = 0; j &lt; dt.Columns.Count; j++)
              {
                  string cellText = Server.HtmlDecode(dt.Rows[i][j].ToString());
                  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("#FFFFFF"));
                  }
                  table.AddCell(cell);
              }
          }
          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=" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".pdf");
          Response.Cache.SetCacheability(HttpCacheability.NoCache);
          Response.Write(pdfDoc);
          Response.End();
  </code></pre>