Skip to content Skip to sidebar Skip to footer

ITextSharp HTMLWorker.ParseToList() Throws NullReferenceException

I am using iTextSharp v.4 to merge a whole bunch of html files. It was working fine until I needed to upgrade to v.5 of iTextSharp. The problem comes when I pass a streamreader (r

Solution 1:

It looks like HTMLWorker is choking on the two <hr width="100%" />. Since you said you're ugrading to V5.XX, it might also be good to start using XMLWorker to start parsing your HTML - the development team is recommending it. (the latest HTMLWorker source code even has a small reference pointing this out)

Tested with your extended HTML, it works, and isn't too bad to implement :)

using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  try {
    StringReader sr = new StringReader(htmlString);
    XMLWorkerHelper.GetInstance().ParseXHtml(
      writer, document, sr
    );          
  }
  catch (Exception e) {
    throw;
  }
}

Tested in a web environment, so replace Response.OutputStream with the Stream of your choice.


Post a Comment for "ITextSharp HTMLWorker.ParseToList() Throws NullReferenceException"