类型1维数组的值无法转换为system.collections.bitarray

时间:2010-03-12 14:22:13

标签: vb.net

我有一个函数来创建一个应该返回bitarray的pdf。以下是代码

Public Function GenPDF() As BitArray
        Dim pdfdoc1 As BitArray

        Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
        Try
            Dim MemStream As New MemoryStream
            Dim wri As PdfWriter = PdfWriter.GetInstance(doc, MemStream)
            'Open Document to write
            doc.Open()

            'Write some content
            Dim paragraph As New Paragraph("This is my first line using Paragraph.")
            Dim pharse As New Phrase("This is my second line using Pharse.")
            Dim chunk As New Chunk(" This is my third line using Chunk.")
            ' Now add the above created text using different class object to our pdf document
            doc.Add(paragraph)
            doc.Add(pharse)
            doc.Add(chunk)
            pdfdoc1 = MemStream.GetBuffer()
        Catch dex As DocumentException


            'Handle document exception

        Catch ex As Exception
            'Handle Other Exception
        Finally
            'Close document
            doc.Close()

        End Try

但它在此行引发错误pdfdoc1 = MemStream.GetBuffer() 类型1维数组的值无法转换为system.collections.bitarray 请帮忙

1 个答案:

答案 0 :(得分:0)

BitArray的MSDN文档中,BitArray类表示类似于布尔数组的内容:

  

管理一个紧凑的位值数组,表示为布尔值,其中true表示该位为on(1),false表示该位为off(0)。

虽然您可能会将此解释为存储1和0,但它实际上是说存储了True和False。

另一方面,MemoryStream.GetBuffer()返回Byte数组。因此,两者不兼容,因为字节不是布尔值。

试试这个:

Public Function GenPDF() As Byte() ' Don't forget to change your return type.
    Dim pdfdoc1 As Byte()          ' Changed this to the correct type.
    Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
    Try
        Dim MemStream As New MemoryStream
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, MemStream)
        'Open Document to write
        doc.Open()

        'Write some content
        Dim paragraph As New Paragraph("This is my first line using Paragraph.")
        Dim pharse As New Phrase("This is my second line using Pharse.")
        Dim chunk As New Chunk(" This is my third line using Chunk.")
        ' Now add the above created text using different class object to our pdf document
        doc.Add(paragraph)
        doc.Add(pharse)
        doc.Add(chunk)
        pdfdoc1 = MemStream.GetBuffer()
    Catch dex As DocumentException
        'Handle document exception
    Catch ex As Exception
        'Handle Other Exception
    Finally
        'Close document
        doc.Close()
    End Try
相关问题