iTextSharp HTMLWorker.ParseToList() throws NullReferenceException

我与影子孤独终老i 提交于 2019-11-29 10:01:34

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!