1

I am using iTextSharp to convert a HTML (source site is google: http://www.google.co.in/) to PDF.

My code :

protected void Page_Load(object sender, EventArgs e)
{  
    WebClient wc = new WebClient();
    string HTMLCode = wc.DownloadString("http://www.google.co.in/");
    var result = createPDF(HTMLCode);            
}

private MemoryStream createPDF(string html)
{
    MemoryStream msOutput = new MemoryStream();
    TextReader reader = new StringReader(html);

    // step 1: creation of a document-object
    Document document = new Document(PageSize.A4, 30, 30, 30, 30);

    // step 2:
    // we create a writer that listens to the document
    // and directs a XML-stream to a file
    PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

    // step 3: we create a worker parse the document
    HTMLWorker worker = new HTMLWorker(document);

    // step 4: we open document and start the worker on the document
    document.Open();
    worker.StartDocument();

    // step 5: parse the html into the document
    worker.Parse(reader);

    // step 6: close the document and the worker
    worker.EndDocument();
    worker.Close();
    document.Close();

    return msOutput;           
}

I have referred the createPDF function from here.

But I am encountering the below error

Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type 'iTextSharp.text.Paragraph'.

Is it some problem with iTextSharp library? By the way I am using itextsharp-dll-core-5.3.0

Community
  • 1
  • 1
Niladri Biswas
  • 4,153
  • 2
  • 17
  • 24

1 Answers1

1

No one listens! HTMLWorker

  • is obsolete.
  • won't be maintained.
  • it's replaced with XML-Worker.
VahidN
  • 18,457
  • 8
  • 73
  • 117
  • It is in which namespace..i could not find XMLWorkerHelper or XMLWorker...please correct me if i am wrong – Niladri Biswas Aug 14 '12 at 12:25
  • XML-Worker is an add-on project right now. you can find it here: http://sourceforge.net/projects/itextsharp/files/xmlworker/ and also its samples are located here: https://itextsharp.svn.sourceforge.net/svnroot/itextsharp/trunk/src/extras/itextsharp.xmlworker.testApps/ – VahidN Aug 14 '12 at 18:41