0

While trying to create HTML to Pdf using itextsharp getting Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'. exception.Here is the piece of code

string FilePath = System.Web.HttpContext.Current.Server.MapPath("~/EmailTemplate/tenantStatement-print.html");
        StreamReader mstrFileStreamReader = new StreamReader(FilePath);

        string fileContents = mstrFileStreamReader.ReadToEnd();

        mstrFileStreamReader.Close();

        String htmlText = fileContents;

        Document document = new Document();

        PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("~\\DocumentEngine\\tempDocs\\") + "\\outfile.pdf", FileMode.Create));
        document.Open();
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(htmlText));
        document.Close();

        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
        Response.ContentType = "application/pdf";
        Response.WriteFile(HttpContext.Current.Server.MapPath("~\\DocumentEngine\\tempDocs\\") + "\\outfile.pdf");
        Response.Flush();
        Response.Clear();
Supreet
  • 2,451
  • 8
  • 25
  • 42
  • 2
    Please understand that `HTMLWorker` has been abandoned. You should use XML Worker instead: http://itextpdf.com/sandbox/xmlworker – Bruno Lowagie Dec 04 '14 at 13:11
  • possible duplicate of [ItextSharp Error on trying to parse html for pdf conversion](http://stackoverflow.com/questions/12113425/itextsharp-error-on-trying-to-parse-html-for-pdf-conversion) – Chris Haas Dec 04 '14 at 14:27

0 Answers0