简体   繁体   English

我需要良好的ITextSharp ASPX到PDF转换示例

[英]I need good ITextSharp ASPX to PDF conversion Examples

I have several ASPX web forms that have many text boxes, radio button lists, combo boxes, and many other fancy Telerik controls. 我有几种ASPX Web窗体,其中包含许多文本框,单选按钮列表,组合框以及许多其他精美的Telerik控件。 After a page is filled out and the submit button is clicked the whole page needs to be converted to a PDF. 填写页面并单击提交按钮后,需要将整个页面转换为PDF。 I need examples of how I could possibly do this with iTextSharp. 我需要一些示例,说明如何使用iTextSharp做到这一点。 I have been messing around with a Test project to see how it works and got the following exception when I added a RadTextBox and a RadComboBox: 我一直在忙于测试项目,以查看其工作方式,并在添加RadTextBox和RadComboBox时遇到以下异常:

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. 无法将类型为“ iTextSharp.text.html.simpleparser.CellWrapper”的对象转换为类型为“ iTextSharp.text.Paragraph”的对象。 Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. 异常详细信息:System.InvalidCastException:无法将类型为“ iTextSharp.text.html.simpleparser.CellWrapper”的对象转换为类型为“ iTextSharp.text.Paragraph”的对象。

Source Error: 源错误:

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);

Stack Trace: 堆栈跟踪:

[InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.] [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

C# Method for PDF Conversion: 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();
}

A solution to the exception would be appreciated. 对该异常的解决方案将不胜感激。 In general, I think I need a better idea of how to use iTextSharp for my web forms that are littered with controls. 总的来说,我认为我需要更好地了解如何将iTextSharp用于充斥控件的Web窗体。

i think if your code is work perfectly then only one problem create with version of itextsharp.dll, 我认为,如果您的代码运行正常,那么itextsharp.dll版本只会造成一个问题,

you should vist this link and download version give in it..then after any code working perfectly, 您应该访问此链接并下载版本。.然后在任何代码正常运行之后,

link 链接

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

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