Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'object reference not set to an instance of an object' for HTMLWorker parser

Tags:

c#

itextsharp

Document document = new Document(PageSize.LETTER, 10, 10, 10, 10);
StringReader reader = new StringReader(edittedHTML);
HTMLWorker worker = new HTMLWorker(document);
string fileName = "test.pdf";
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
worker.Parse(reader);
worker.EndDocument();
worker.Close();
document.Close();

When the program runs to worker.Parse, it throws out an error just like the title said.

The edtted HTML is the HTML string of an HTML page.

Anyone know how to solve this, or what is going wrong?

The stack trace:

at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)
at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)
at TestPdfApplication.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\TLiu\Documents\Visual Studio 2010\Projects\TestPdfApplication\TestPdfApplication\Form1.cs:line 68
like image 322
Tiger Avatar asked May 28 '13 19:05

Tiger


2 Answers

I think the problem is a null reference exception being thrown due to HTML tags that the parser was unable to handle. try to remove the tags although HTMLWorker is no longer supported. It's discontinued in favor of XML Worker

like image 61
Feras Salim Avatar answered Oct 23 '22 03:10

Feras Salim


It looks like a null reference. Try to use the sintaxis "using" with all the IDisposable items:

using (HTMLWorker worker = new HTMLWorker(document))
                { (......) }
like image 36
Ignacio Avatar answered Oct 23 '22 03:10

Ignacio