title: converting gridview to itextsharp table and generating pdf using asp.net
body: <p>here i am trying to convert GridView to PDF, when converting PDF not getting the GridView column width in PDF.So i used another method to generate PDF (i.e) created itextsharp table from GridView .Here i got column width size which i gave in GridView column ItemStyle-Width.But i have done grouped column in that GridView so i am not getting the full GridView with same column width in PDF.can anyone please help me . </p>
  
  <p>here is the source:</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
       onsorting="GridView1_Sorting"&gt;
       &lt;HeaderStyle BackColor="#4F81BD" Font-Bold="true" ForeColor="#ffffff" HorizontalAlign="Center" Font-Size="12px"/&gt;
      &lt;AlternatingRowStyle BackColor="#D3DFEE" /&gt;
      &lt;RowStyle /&gt;
      &lt;Columns&gt;
           &lt;asp:BoundField DataField="Category" HeaderText="Category"
              SortExpression="Category" ItemStyle-Width="100px"/&gt;
              &lt;asp:BoundField DataField="Partno" HeaderText="Partno" 
               ItemStyle-Width="100px"/&gt;
              &lt;asp:BoundField DataField="Productname" HeaderText="Productname" 
               ItemStyle-Width="300px"/&gt;
              &lt;asp:BoundField DataField="Brand" HeaderText="Brand" 
              ItemStyle-Width="100px"/&gt;
              &lt;asp:BoundField DataField="Qty" HeaderText="Qty" 
               ItemStyle-Width="50px"/&gt;
              &lt;asp:BoundField DataField="Unitprice" HeaderText="Unitprice" 
               ItemStyle-Width="50px"/&gt;
              &lt;asp:BoundField DataField="Totalprice" HeaderText="Totalprice" 
               ItemStyle-Width="50px"/&gt;
      &lt;/Columns&gt;
  
  &lt;/asp:GridView&gt;  
  </code></pre>
  
  <p>here is the grouped gridview:</p>
  
  <p><img src="https://i.stack.imgur.com/vkaiI.jpg" alt="enter image description here"></p>
  
  <p>here is the code to convert gridview to itextsharp table and generating PDF:</p>
  
  <pre><code>//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 &lt; 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 iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#008000"));
      table.AddCell(cell);
  }
  
  table.SetWidths(widths);
  //Transfer rows from GridView to table
  
  for (int i = 0; i &lt; GridView1.Rows.Count; i++)
  {
      if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
      {
          for (int j = 0; j &lt; GridView1.Columns.Count; j++)
          {
              string cellText = Page.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 iTextSharp.text.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();
  </code></pre>
  
  <p>here is the PDF:</p>
  
  <p><img src="https://i.stack.imgur.com/EIqQ4.png" alt="enter image description here"></p>
  
  <p>i want exact gridview with same column width in PDF.</p>