iTextSharp在htmlWorker.Parse上生成InvalidCastException(iTextSharp generating InvalidCastException on htm

编程入门 行业动态 更新时间:2024-10-10 00:29:04
iTextSharp在htmlWorker.Parse上生成InvalidCastException(iTextSharp generating InvalidCastException on htmlWorker.Parse)

当试图将GridView转换为PDF时,我在线上使用iTextSharp时收到InvalidCastException

htmlWorker.Parse(stringReader)

我究竟做错了什么? 我搜索过,每个人似乎都是这样做的。 这是错误。

无法将“iTextSharp.text.html.simpleparser.CellWrapper”类型的对象强制转换为“iTextSharp.text.Paragraph”。

这是我的iTextSharp包含

using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;

这是我的代码。

public override void VerifyRenderingInServerForm(Control control) { } protected void LinkButton2_Click(object sender, EventArgs e) { Response.ClearContent(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=PDFDoc.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); GridView1.RenderControl(htmlTextWriter); StringReader stringReader = new StringReader(stringWriter.ToString()); Document pdfDoc = new Document(PageSize.LETTER, 10f, 10f, 10f, 0f); HTMLWorker htmlWorker = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlWorker.Parse(stringReader); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }

When attempting to turn a GridView into a PDF I am getting an InvalidCastException when using iTextSharp on the line

htmlWorker.Parse(stringReader)

What am I doing wrong? I searched and everyone seems to do this the same way. Here is the error.

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.

Here are my includes for iTextSharp

using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;

Here is my code.

public override void VerifyRenderingInServerForm(Control control) { } protected void LinkButton2_Click(object sender, EventArgs e) { Response.ClearContent(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=PDFDoc.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); GridView1.RenderControl(htmlTextWriter); StringReader stringReader = new StringReader(stringWriter.ToString()); Document pdfDoc = new Document(PageSize.LETTER, 10f, 10f, 10f, 0f); HTMLWorker htmlWorker = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlWorker.Parse(stringReader); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }

最满意答案

在GridView1.RenderControl(htmlTextWriter)之前,我不得不放下以下代码。

GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.DataBind();

然后它就像一个魅力。

Before GridView1.RenderControl(htmlTextWriter) I had to put the following code.

GridView1.AllowPaging = false; GridView1.AllowSorting = false; GridView1.DataBind();

Then it worked like a charm.

更多推荐

本文发布于:2023-08-07 17:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:htmlWorker   iTextSharp   Parse   generating   InvalidCastException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!