iTextSharp - 使用MemoryStream for PDFs的XMLWorkerHelper GetInstance出错

时间:2016-01-22 21:22:32

标签: vb.net itextsharp byte

我在网页上有一个占位符,我需要将其转换为pdf(字节)。我的代码工作正常,但由于某种原因,它给了我一个错误。

以前,代码包含以下行:

“使用writer = PdfWriter.GetInstance(doc,ms)”但是生成了DataStream的ERROR已关闭“所以我改变了代码,如下所示。新的ERROR是Object未被引用。

任何帮助都将不胜感激。

错误:
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance()。ParseXHtml(writer,doc,msHtml,msCss)

错误消息:“对象引用未设置为对象的实例。”

Protected Sub createPDF()

    Dim stringWriter As New StringWriter()
    Dim htmlTextWriter As New HtmlTextWriter(stringWriter)
    PlaceHolder1.RenderControl(htmlTextWriter)

    'Create a byte array that will eventually hold our final PDF
    Dim bytes As [Byte]()

     Using ms = New MemoryStream()

      'Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
      Using doc = New Document()

       'Using writer = PdfWriter.GetInstance(doc, ms)
        Dim writer As iTextSharp.text.pdf.PdfWriter
        writer = PdfWriter.GetInstance(doc, ms)

            'Open the document for writing
            doc.Open()

            Dim example_html = htmlTextWriter.InnerWriter.ToString()
            Dim example_css = ".headline{font-size:200%}"

            Using msCss = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css))
                Using msHtml = New MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html))
                    'Error occurs here, next line
                    iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss)
                End Using
            doc.Close()
         End Using
    End Using

    bytes = ms.ToArray()
    'store into variable
    DocuSign.DocusignContracts.m_AddendumByte = ms.ToArray()
    DocuSign.DocusignContracts.m_AddendumLen = ms.ToArray().Length

    ms.Close()
End Using

End Sub

0 个答案:

没有答案