0

There is an exception occurred in the line XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDoc, sr)

An exception of type 'System.NullReferenceException' occurred in itextsharp.xmlworker.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

when actually each parameter has been clearly set to an instance.

Dim pdfDoc As Document = New Document(PageSize.A4, 10, 10, 10, 10)
Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()

Dim b = Convert.FromBase64String(HttpContext.Current.Request.Form("code"))
Dim html = System.Text.Encoding.UTF8.GetString(b)
MsgBox(html)
Dim sr As StringReader = New StringReader(html)
MsgBox(IsNothing(pdfDoc).ToString()) 'False
MsgBox(IsNothing(pdfWriter).ToString()) 'False
MsgBox(IsNothing(sr).ToString()) 'False
XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDoc, sr)
pdfDoc.Close()

This exception occurs only when html contains tags, if it is only with plain text, no such exception occurs. For example, if I have set html = "<td></td>", exception will occurs; if I remove the / to make the string invalid as HTML code, exception won't occur.

But as my purpose is to parse an HTML page into PDF, html must be stringified source code, with a lot of HTML tags.

Ursidae
  • 87
  • 9
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – OlegI Aug 06 '19 at 09:42
  • Does `pdfWriter.GetInstance(pdfDoc, Response.OutputStream)` always return an instance? can it return `Nothing` in some cases? Have you stepped trough the code with the debugger to make sure that indeed no object has been set to `Nothing`? – AConfusedSimpleton Aug 06 '19 at 10:03
  • Just edited the question. I don't think it's something the exception literally means. – Ursidae Aug 06 '19 at 10:13
  • from [this](https://stackoverflow.com/questions/30916122/itextsharp-tool-xml-xmlworkerhelper-getinstance-parsexhtml-not-working) question: try calling `pdfDoc.Close()` before `ParseXHtml` – AConfusedSimpleton Aug 06 '19 at 10:18

0 Answers0