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.