Hi ashokpumca,
Please take reference to below code and correct your code.
HTML
<div id="dvHtml" runat="server" style="margin: 0 0 0 0;">
<asp:Button ID="btn" runat="server" Text="Print" OnClick="ButnPrint_Click" />
<table style="text-align: left; width: 100%; font-size: 11px; background-color: white"
border="0" cellpadding="6" cellspacing="0">
<tr>
<td>
Report Name :
<asp:Label ID="lblReport" runat="server" Text=""></asp:Label>
</td>
<td>
Report Generated At :
<asp:Label ID="lblReptAt" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td>
Report Generated By :
<asp:Label ID="lblReptBy" runat="server" Text=""></asp:Label>
</td>
<td>
Machine IP :
<asp:Label ID="lblReptIP" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerID"
Width="100%" Font-Size="9">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID">
<ItemStyle Width="20px" Height="25px" />
</asp:BoundField>
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName">
<ItemStyle Width="450px" />
</asp:BoundField>
<asp:BoundField DataField="ContactName" HeaderText="ContactName">
<ItemStyle Width="50" />
</asp:BoundField>
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle">
<ItemStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Address" HeaderText="Address">
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="City" HeaderText="City">
<ItemStyle Width="350px" />
</asp:BoundField>
</Columns>
<HeaderStyle Font-Bold="True" Font-Names="Trebuchet MS" Font-Size="10" />
</asp:GridView>
</td>
</tr>
</table>
</div>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Web.UI.WebControls;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void ButnPrint_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename= report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Label label = new Label();
gvCustomers.Parent.Controls.Add(label);
label.Controls.Add(gvCustomers);
dvHtml.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.LEGAL, 5, 5, 5, 5);
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)
{
/* Verifies that the control is rendered */
}