Error on converting GridView to .pdf file.http://forums.asp.net/t/1793582.aspx/1?Error+on+converting+GridView+to+pdf+file+Tue, 17 Apr 2012 05:02:47 -040017935824935750http://forums.asp.net/p/1793582/4935750.aspx/1?Error+on+converting+GridView+to+pdf+file+Error on converting GridView to .pdf file. <p>I am currently trying to convert GridView that is generated into a pdf file. Unfortunately, I am constantly getting this error:</p> <p>&quot;Unable to cast object of type 'ITextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.</p> <p>I will include the code for reference and I really hope any of u guys can point ou the cause of the error.</p> <pre class="prettyprint">.cs:</pre> <pre class="prettyprint">protected void GenerateReport(object sender, EventArgs e)</pre> <pre class="prettyprint">{</pre> <pre class="prettyprint"> Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); HtmlForm frm = new HtmlForm(); GridView1.AllowPaging = false; GridView1.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(GridView1); frm.RenderControl(hw); GridView1.DataBind(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); pdfDoc.Close(); Response.Write(pdfDoc); Response.End();</pre> <pre class="prettyprint">}</pre> <pre class="prettyprint">.aspx:</pre> <pre class="prettyprint">&nbsp;Report Format:&lt;asp:DropDownList ID="ddlReportFormat" runat="server"&gt; &lt;asp:ListItem Text="Please Select A Format" Value="default"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="Excel" Value="1"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="Word" Value="2"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="PDF" Value="3"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID="repSel" runat="server" Text="Generate Report" OnClick="GenerateReport"/&gt;</pre> <p>Thanks in advance!</p> 2012-04-17T04:31:30-04:004935791http://forums.asp.net/p/1793582/4935791.aspx/1?Re+Error+on+converting+GridView+to+pdf+file+Re: Error on converting GridView to .pdf file. <p>Hi,</p> <p></p> <p>Basically, the problem is not your code. It's the HTML that you are passing to iTextSharp. There is most likely something it doesn't lik in the HTML</p> <p></p> <p>check code in below</p> <p></p> <pre class="prettyprint">protected void btnExportPDF_Click(object sender, EventArgs e) { Response.ContentType = &quot;application/pdf&quot;; Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=GridViewExport.pdf&quot;); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }</pre> <p></p> <p>Link</p> <p><a href="http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx">http://www.aspsnippets.com/Articles/Export-GridView-To-Word-Excel-PDF-CSV-Formats-in-ASP.Net.aspx</a></p> <p><a href="http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx">http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx</a><br> <br> </p> 2012-04-17T05:02:47-04:00