{"id":5944,"date":"2014-04-10T08:54:25","date_gmt":"2014-04-10T08:54:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/10\/i-need-good-itextsharp-aspx-to-pdf-conversion-examples-collection-of-common-programming-errors\/"},"modified":"2014-04-10T08:54:25","modified_gmt":"2014-04-10T08:54:25","slug":"i-need-good-itextsharp-aspx-to-pdf-conversion-examples-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/10\/i-need-good-itextsharp-aspx-to-pdf-conversion-examples-collection-of-common-programming-errors\/","title":{"rendered":"I need good ITextSharp ASPX to PDF conversion Examples-Collection of common programming errors"},"content":{"rendered":"<p>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:<\/p>\n<p>Unable to cast object of type &#8216;iTextSharp.text.html.simpleparser.CellWrapper&#8217; to type &#8216;iTextSharp.text.Paragraph&#8217;. 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.<\/p>\n<p>Exception Details: System.InvalidCastException: Unable to cast object of type &#8216;iTextSharp.text.html.simpleparser.CellWrapper&#8217; to type &#8216;iTextSharp.text.Paragraph&#8217;.<\/p>\n<p>Source Error:<\/p>\n<pre><code>Line 33:             PdfWriter.GetInstance(pdfDoc, Response.OutputStream);\nLine 34:             pdfDoc.Open();\nLine 35:             htmlparser.Parse(sr);\nLine 36:             pdfDoc.Close();\nLine 37:             Response.Write(pdfDoc);\n<\/code><\/pre>\n<p>Stack Trace:<\/p>\n<p>[InvalidCastException: Unable to cast object of type &#8216;iTextSharp.text.html.simpleparser.CellWrapper&#8217; to type &#8216;iTextSharp.text.Paragraph&#8217;.]<\/p>\n<pre><code>iTextSharp.text.html.simpleparser.HTMLWorker.ProcessLink() +563\niTextSharp.text.html.simpleparser.HTMLTagProcessor_A.EndElement(HTMLWorker worker, String tag) +36\niTextSharp.text.html.simpleparser.HTMLWorker.EndElement(String tag) +80\niTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start) +136\niTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1950\niTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html) +83\niTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader) +59\nWebApplication5._Default.btnExport_Click(Object sender, EventArgs e) in C:\\Users\\jensenty\\Documents\\Visual Studio 2008\\Projects\\WebApplication5\\WebApplication5\\Default.aspx.cs:35\nSystem.Web.UI.WebControls.Button.OnClick(EventArgs e) +111\nSystem.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110\nSystem.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10\nSystem.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13\nSystem.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36\nSystem.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565\n<\/code><\/pre>\n<p>C# Method for PDF Conversion:<\/p>\n<pre><code>protected void btnExport_Click(object sender, EventArgs e)\n{\n    Response.ContentType = \"application\/pdf\";\n    Response.AddHeader(\"content-disposition\", \"attachment;filename=TestPage.pdf\");\n    Response.Cache.SetCacheability(HttpCacheability.NoCache);\n    StringWriter sw = new StringWriter();\n    HtmlTextWriter hw = new HtmlTextWriter(sw);\n    this.Page.RenderControl(hw);\n    StringReader sr = new StringReader(sw.ToString());\n    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);\n    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);\n    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);\n    pdfDoc.Open();\n    htmlparser.Parse(sr);\n    pdfDoc.Close();\n    Response.Write(pdfDoc);\n    Response.End();\n}\n<\/code><\/pre>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5944","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5944"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5944\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}