1

I'm getting a

Object reference not set to an instance of an object.

error while using HTMLWorker.ParseToList from iTextSharp. Here a part of my C# code

string htmlText = Server.UrlDecode(content);

Document document = new Document();

StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle("box_wrap", "border", "1px solid #333");
styles.LoadTagStyle("box_wrap", "line-height", "22px");
styles.LoadTagStyle("box_wrap", "font-size", "12px");
styles.LoadTagStyle("li", "list-style", "none");

PdfWriter.GetInstance(document, new FileStream(strSavePath, FileMode.Create));

document.Open();
StringReader sr = new StringReader(htmlText);
List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), styles);

HTMLWorker.ParseToList throws the error.

Here is the htmlText which i'm passing into StringReader. Here

UPDATE: removing the hr as mentioned by @Chris in comments resolved the above error but gave way to another error Illegal characters in path

iJade
  • 23,144
  • 56
  • 154
  • 243
  • http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Soner Gönül Apr 08 '14 at 11:23
  • @SonerGönül here i'm passing in 2 parameters to `HTMLWorker.ParseToList`...both which are not null. I double checked.Still getting this error – iJade Apr 08 '14 at 11:25
  • `HTMLWorker` is very old, has a few bugs and has been deprecated in favor of `XMLWorker`. `HTMLWorker` doesn't support most CSS and isn't being maintained. I've seen the issue you're reporting come up several times and often it is an `
    ` tag that breaks things. Try turning `htmlText` into a literal string and start pruning that until things no longer break.
    – Chris Haas Apr 08 '14 at 21:49
  • @ChrisHaas thnks dat helped. But it gave another error but finally resolved the issue. posted answer – iJade Apr 11 '14 at 05:15

1 Answers1

0

In my html I had an image whose path was set as shown below:

<img id="img1" runat="server" alt="" src="../Images/hello.png" />

which I changed to

<img id="img1" runat="server" alt="" src="http://localhost:1605/Images/hello.png" />

This resolved the error Illegal characters in path

iJade
  • 23,144
  • 56
  • 154
  • 243
  • You feed the HTML to iTextSharp using a `StringReader`, so how should it interpret something like `..`? – mkl Apr 11 '14 at 21:20