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

#696620    Author: gandhi  03 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


Regards,
Gandhi

#696685    Author: Ravindran    04 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 = "<h5>EXPORT HTML CONTENT TO PDF</h5><br/><br/><b><u>This content is convert from html string to PDF</u></b><br/><br/><br/><font color='red'>Samples from Ravi!!!</font>";
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());
}
}
}

Regards
N.Ravindran
Your Hard work never fails


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.