You must Sign In to post a response.

Unable to cast object of type 'iTextSharp.text.html.simpleparser.TableWrapper' to type 'iT

Exception
Unable to cast object of type 'iTextSharp.text.html.simpleparser.TableWrapper' to type 'iTextSharp.text.Meta'


when coding to Convert htmlText to pdfFormat
foreach (IElement element in HTMLWorker.ParseToList(new StringReader(htmlText), null))
{
doc.Add(element); // this point error is throwing

}


Comments

Author: gandhi03 Dec 2012 Member Level: Gold   Points : 0

Hi.,

Refer these sites.Hopefully you will get solution.

http://forums.asp.net/t/1510535.aspx/1

http://hspinfo.wordpress.com/2008/01/12/how-to-convert-html-content-to-pdf-file/

http://netpl.blogspot.in/2008/02/unable-to-cast-object-of-type-to-type.html

Author: Ravindran04 Dec 2012 Member Level: Diamond   Points : 4

Hi,

make sure you include namespace using iTextSharp.text.pdf; and using iTextSharp.text.html.simpleparser;

try like this code to convert pdf using itextsharp


using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnExportPDF_Click(object sender, EventArgs e)
{
Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
try
{
PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//Assign Html content in a string to write in PDF
string contents = "
EXPORT HTML CONTENT TO PDF


This content is convert from html string to PDF


Samples from Ravi!!!";
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
pdfDoc.Add(htmlElement as IElement);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}


Sign In to post your comments