PDF only export GridView in aspx pages it doesnt export tables.
I have a code that export aspx pages to pdf very well. The problem is that it only export GridView in the aspx pages.
I tried adding the table id of the table in the page it's name is (use) to the RenderControl(hw),
it keeps saying the name use does not exist in the current context
My code
<table style="width: 34%;" id="use">
<tr>
<td bgcolor="#0099CC" class="style1">
Name
</td>
<td bgcolor="#0099CC" class="style2">
Aaron
</td>
<td bgcolor="#0099CC" class="style3">
Height
</td>
<td bgcolor="#0099CC">
7ft
</td>
</tr>
<tr>
<td bgcolor="#0099CC" class="style1">
Age
</td>
<td bgcolor="#0099CC" class="style2">
23
</td>
<td bgcolor="#0099CC" class="style3">
Hair Colour
</td>
<td bgcolor="#0099CC">
Blue
</td>
</tr>
<tr>
<td bgcolor="#0099CC" class="style1">
Sex
</td>
<td bgcolor="#0099CC" class="style2">
Male
</td>
<td bgcolor="#0099CC" class="style3">
Race
</td>
<td bgcolor="#0099CC">
White
</td>
</tr>
</table>
<br />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#666666" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#E4E4E4" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="100px" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120px" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120px" />
</Columns>
</asp:GridView>
protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}