简体   繁体   English

将GridView导出为PDF时出现ITextSharp错误

[英]ITextSharp Error while exporting GridView to PDF

I tried exporting a GridView to PDF. 我尝试将GridView导出为PDF。 It gives the error: 它给出了错误:

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. 无法将类型为“ iTextSharp.text.html.simpleparser.CellWrapper”的对象转换为类型为“ iTextSharp.text.Paragraph”的对象。

It throws the error here 它在这里抛出错误

htmlparser.Parse(sr);

You should disable sorting and paging before the databinding and parsing. 您应该在数据绑定和解析之前禁用排序和分页。

Here is the code: 这是代码:

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition","attachment;filename=GridViewExport.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;     <----

GridView1.AllowSorting = false;    <----

GridView1.DataBind();

GridView1.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM