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 (reading the content of the html file) into the HTMLWorker object's ParseToList method. It throws a null reference exception. On debugging it I can access the streamReader and can confirm that the correct content of the file is read.

Here is the code:

List<IElement> objects;
try
{ objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}
catch (Exception e)
{ htmlString = "<html><head></head><body><br/><br/><h2 style='color:#FF0000'>ERROR READING FILE!</h2><h3>File Excluded From Stitched Document!</h3><br/><br/><p>There was an error while trying to read the following file:</p><p><span style='color:#FF0000'>" + fileName + "</span></p></body></html>"; objects = HTMLWorker.ParseToList(new StringReader(htmlString), null);
}

In the catch block you will see that I then use practically the same code to add text to the pdf to say that there was a problem. This code works fine. This of course makes me think that the problem lies in the content of the original html string, so here is the content of the string as it is immediately before being passed into the parser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="cache-control" content="no-cache" />
</head>
<body> <div> <h1> Advice Item 1</h1> <table border="0"> <tbody> <tr> <td> &nbsp; </td> <td> </td> <td> &nbsp; </td> <td> </td> </tr> <tr> <td colspan="4"> <span></span> </td> </tr> <tr> <th scope="row"> By: </th> <td> ABC </td> <th scope="row"> From: </th> <td> CC </td> </tr> <tr> <th scope="row"> Date: </th> <td> 29/03/2011 13:35 </td> <th scope="row"> To: </th> <td> Member Practice </td> </tr> <tr> <th scope="row"> Folder: </th> <td> A15-123456 </td> <th scope="row"> Individual: </th> <td> Miss A B Test </td> </tr> <tr> <td colspan="2"> <hr width="100%" /> </td> <th scope="row"> Of: </th> <td> Lorem &amp; Ipsum </td> </tr> <tr> <th scope="row"> Species: </th> <td> Bovine </td> <th scope="row"> Position: </th> <td> Member </td> </tr> <tr> <th scope="row"> Item Type: </th> <td> </td> <th scope="row"> Tel: </th> <td> 0123 01234 </td> </tr> <tr> <th scope="row"> </th> <td> </td> <th scope="row"> Other Nos: </th> <td> </td> </tr> <tr> <th scope="row"> Reason For Call: </th> <td colspan="3"> Some Reason </td> </tr> <tr> <th scope="row"> Subject: </th> <td colspan="3"> Some problem. </td> </tr> <tr> <th scope="row"> </th> <td> </td> <th scope="row" colspan="2"> </th> <td colspan="2"> </td> </tr> <tr> <td colspan="4"> Internal </td> </tr> <tr> <td colspan="4"> <hr width="100%" /> </td> </tr> </tbody> </table> <div> <p> Here we start the discussion.</p> <br /> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <br /> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> </div>
</body>
</html>

Thanks for any help. hofnarwillie

1 Answer

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.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like