简体   繁体   中英

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. After a page is filled out and the submit button is clicked the whole page needs to be converted to a PDF. I need examples of how I could possibly do this with 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:

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. Description: An unhandled exception occurred during the execution of the current web request. 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'.

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'.]

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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