In code behind add these references:-
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
After that write the following code in code behind:-
public override void VerifyRenderingInServerForm(Control control)
{
// Verifies that the control is rendered
}
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "25%");
gvdetails.HeaderRow.Style.Add("font-size", "11px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "9px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Am getting this Error after "htmlparser.parse(sr)"
ReplyDeleteUnable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.
Thank You
Good post.need to give more explanation.given is Solutions for HTML parser
ReplyDeletehttp://www.aspdotnet-sharepoint.com/2013/05/how-to-export-gridview-data-to-pdf-in.html
C# export GridView to PDF file using KetticGridView,
ReplyDeleteExportToPdf exporter = new ExportToPdf(this.ketticGridView1);