title: I need good ITextSharp ASPX to PDF conversion Examples
body: <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>
  
  <p>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. </p>
  
  <p>Exception Details: System.InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.</p>
  
  <p>Source Error:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Stack Trace: </p>
  
  <p>[InvalidCastException: Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.]</p>
  
  <pre><code>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
  </code></pre>
  
  <p>C# Method for PDF Conversion:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <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>