0

我有几个 ASPX Web 表单,其中包含许多文本框、单选按钮列表、组合框和许多其他精美的 Telerik 控件。填写页面并单击提交按钮后,需要将整个页面转换为 PDF。我需要有关如何使用 iTextSharp 做到这一点的示例。我一直在搞乱一个测试项目,看看它是如何工作的,当我添加一个 RadTextBox 和一个 RadComboBox 时,得到了以下异常:

无法将“iTextSharp.text.html.simpleparser.CellWrapper”类型的对象转换为“iTextSharp.text.Paragraph”类型。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidCastException:无法将“iTextSharp.text.html.simpleparser.CellWrapper”类型的对象转换为“iTextSharp.text.Paragraph”类型。

源错误:

Line 33:             PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 34:             pdfDoc.Open();
Line 35:             htmlparser.Parse(sr);
Line 36:             pdfDoc.Close();
Line 37:             Response.Write(pdfDoc);

堆栈跟踪:

[InvalidCastException:无法将“iTextSharp.text.html.simpleparser.CellWrapper”类型的对象转换为“iTextSharp.text.Paragraph”类型。]

iTextSharp.text.html.simpleparser.HTMLWorker.ProcessLink() +563
iTextSharp.text.html.simpleparser.HTMLTagProcessor_A.EndElement(HTMLWorker worker, String tag) +36
iTextSharp.text.html.simpleparser.HTMLWorker.EndElement(String tag) +80
iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start) +136
iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1950
iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html) +83
iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader) +59
WebApplication5._Default.btnExport_Click(Object sender, EventArgs e) in C:\Users\jensenty\Documents\Visual Studio 2008\Projects\WebApplication5\WebApplication5\Default.aspx.cs:35
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

PDF转换的C#方法:

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Page.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

将不胜感激该异常的解决方案。一般来说,我认为我需要更好地了解如何将 iTextSharp 用于我的充满控件的 Web 表单。

4

1 回答 1

0

i think if your code is work perfectly then only one problem create with version of itextsharp.dll,

you should vist this link and download version give in it..then after any code working perfectly,

link

于 2013-11-13T12:52:20.230 回答