Unable to cast object of type 'iTextSharp.text.html.simpleparser.IncTable' to type 'iTextSharp.text.IElement

Last Reply on Dec 17, 2015 04:50 AM By aditya2267

Posted on Dec 17, 2015 04:10 AM

Hi,

    I want to generate PDF at run time from my .net application. I have followed Author- Mudassar Khan's 1 of post for generating PDF from Asp.net.

But, I receiving 1 error while running the code.

Below is the error - 

Unable to cast object of type 'iTextSharp.text.html.simpleparser.IncTable' to type 'iTextSharp.text.IElement

 

 

StringBuilder sb = new StringBuilder();

                        // Header

                        sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");

                        sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Data</b></td></tr>");
                        sb.Append("<tr><td colspan = '2'></td></tr>");
                        sb.Append("<tr><td colspan = '2' ><b>City: </b>");
                        sb.Append(drpCity.Text);
                        sb.Append("</td></tr>");
                        sb.Append("<tr><td colspan = '2'><b>State : </b>");
                        sb.Append(drpState.Text);
                        sb.Append("</td></tr>");
                        sb.Append("<tr><td colspan = '2'><b>Equipment : </b>");
                        sb.Append(drpEquip.Text);
                        sb.Append("</td></tr>");
                        sb.Append("</table>");
                        sb.Append("<br />");


                        sb.Append("<table border = '1'>");
                        sb.Append("<tr>");

                        foreach (DataColumn column in dt.Columns)
                        {
                            sb.Append("<th style = 'background-color: #D20B0C;color:#ffffff'>");
                            sb.Append(column.ColumnName);
                            sb.Append("</th>");
                        }

                        sb.Append("</tr>");
                        foreach (DataRow row in dt.Rows)
                        {
                            sb.Append("<tr>");
                            foreach (DataColumn column in dt.Columns)
                            {
                                sb.Append("<td>");
                                sb.Append(row[column]);
                                sb.Append("</td>");
                            }
                            sb.Append("</tr>");
                        }

                       

                        //Export HTML String as PDF.
                        StringReader sr = new StringReader(sb.ToString());
                        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                        pdfDoc.Open();
                        htmlparser.Parse(sr);
                        pdfDoc.Close();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "attachment;filename=Data.pdf");
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.Write(pdfDoc);
                        Response.End();

Where Im doing wrong. Please, tell me.

Posted on Dec 17, 2015 04:50 AM

Hi aesha10,

You have not closed the table tag and that what giving error. After foreach loop of Rows append Table Closing tag that will solve the issue.

Thanks