在 VB.NET 中将 PDF 加载到 MemoryStream

Load PDF to MemoryStream in VB.NET

我正在使用 this code 将我的 XFA 数据读取到我的 vb.net 应用程序。但是,我不知道如何将 populatedPDFrom 加载到应用程序。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim filePath As String

        filePath = "d:\waiver_testing\test_pdf\rfrprg67.pdf"

        TextBox1.Text = Export(filePath)

    End Sub

Public Shared Function Export(populatedPDFForm As System.IO.Stream) As System.IO.MemoryStream

        '  Exports XFA data from a PDF File
        '  <param name="populatedPDFForm">a readable stream of the PDF with a populated form</param>
        '  <returns>A stream containing the exported XML form data</returns>

        Dim outputStream As New System.IO.MemoryStream()
        Dim reader As New iTextSharp.text.pdf.PdfReader(populatedPDFForm)
        Dim settings As XmlWriterSettings = New XmlWriterSettings
        settings.Indent = True
        'settings.OmitXmlDeclaration = True

        Using writer = XmlWriter.Create(outputStream, settings)
            reader.AcroFields.Xfa.DatasetsNode.WriteTo(writer)
        End Using

        Return outputStream

    End Function

在线错误: textbox1.text = Export(filePath)

错误: Value type "String" cannot be converted to to "System.IO.Stream"

有人可以给我一根骨头吗?

嗯,您不能将 MemoryStream 分配给字符串。你需要先转换它。例如,要将流读取为 Ascii,您可以使用 Encoding.ASCII.GetString.
此外,您的 Export 函数似乎将流作为参数而不是文件路径。要处理该写入:

Using inStream As Stream = File.OpenRead(filePath)
  TextBox1.Text = Encoding.ASCII.GetString(Export(inStream).ToArray())
End Using