Hi dharmendr,
I later tried this code and it is working, But the gridview width is not 100% in PDF and height is not the same as the one in the HTML
CODE:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
using System.Text;
using iText.Html2pdf;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item"), new DataColumn("Qty"), new DataColumn("Rate"), new DataColumn("Amount") });
dt.Rows.Add("Supply Of Electrical Equipments", "20", "3000", "60000");
dt.Rows.Add("Construction of Bore Hole", "2", "350000", "700000");
dt.Rows.Add("Construction of Fish Pond", "7", "45000", "315000");
dt.Rows.Add("Purcahse of Water Pump", "2", "70000", "140000");
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
}
public void generatePDF()
{
string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
Response.Clear();
Response.ContentType = "Application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
HtmlConverter.ConvertToPdf(getPanelHtml(), Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
public string getPanelHtml()
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
Panel1.RenderControl(hw);
var html = sb.ToString();
return html;
}
protected void Button1_Click(object sender, EventArgs e)
{
generatePDF();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}