在使用 iTextSharp 时尝试解析要转换为 PDF 的 html 字符串时,我的 TextReader 遇到了一点问题。
Function ViewDeliveryNote(ByVal id As Integer) As FileStreamResult
'Memory buffer
Dim ms As MemoryStream = New MemoryStream()
'the document
Dim document As Document = New Document(PageSize.A4)
'the pdf writer
PdfWriter.GetInstance(document, ms)
Dim wc As WebClient = New WebClient
Dim htmlText As String = wc.DownloadString("http://localhost:59800/Warehouse/DeliveryNote/" & id) 'Change to live URL
Dim worker As html.simpleparser.HTMLWorker = New html.simpleparser.HTMLWorker(document)
Dim reader As TextReader = New StringReader(htmlText)
document.Open()
worker.Open()
worker.StartDocument()
worker.Parse(reader)
worker.EndDocument()
worker.Close()
document.Close()
'ready the file stream
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=DeliveryNote.pdf")
Response.Buffer = True
Response.Clear()
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer.Length)
Response.OutputStream.Flush()
Response.End()
Return New FileStreamResult(Response.OutputStream, "application/pdf")
End Function
即使已成功读取 HTML 页面,它停止的行仍worker.Parse(reader)
显示错误。Object reference not set to an instance of an object
StringReader(htmlText)
我不确定自己做错了什么或目前缺少什么,因此我将不胜感激。
更新我只是尝试过Dim reader As New StringReader(htmlText)
,但无济于事。虽然 htmlText 仍然肯定包含一个值,但对象认为它没有。