将PDF文件转换为图片,以便临时读取QR码。

5
我需要能够从PDF文件中读取QR码。我正在使用thoughtworks.QRCode,它接受图像并返回QR码中包含的数据。我已经完成了这部分内容。
但是,我需要能够接受多页PDF文件,并将每一页作为图像发送给QR阅读器。然后,我需要将原始PDF的每一页保存为以QR码中包含的数据命名的单页PDF。
您会推荐我使用哪个库来完成这个项目?许多我看到的库会创建永久性图像,但我只想要临时图像。有没有什么东西可以让我轻松地做到这一点?或者也许有另一个可以读取pdf的QR阅读器?
感谢您提供的任何建议!
1个回答

5
我使用itextsharp和libtiff.NET从PDF文件中提取tiff图像到内存中。总的来说,itextsharp将为您提供对图像的访问权限,但是如果它们被编码,则需要自己进行编码或使用另一个库,这就是libtiff.NET的作用所在。
以下代码基于我提出的问题的答案进行了修改:PDF Add Text and Flatten
Private Shared Function ExtractImages(ByVal pdf As Byte()) As List(Of Byte())
    Dim images As New List(Of Byte())
    Dim reader As New PdfReader(pdf)

    If (reader IsNot Nothing) Then
        ' Loop through all of the references in the PDF.
        For refIndex = 0 To (reader.XrefSize - 1)
            ' Get the object.
            Dim obj = reader.GetPdfObject(refIndex)

            ' Make sure we have something and that it is a stream.
            If (obj IsNot Nothing) AndAlso obj.IsStream() Then
                ' Cast it to a dictionary object.
                Dim pdfDict = DirectCast(obj, iTextSharp.text.pdf.PdfDictionary)

                ' See if it has a subtype property that is set to /IMAGE.
                If pdfDict.Contains(iTextSharp.text.pdf.PdfName.SUBTYPE) AndAlso (pdfDict.Get(iTextSharp.text.pdf.PdfName.SUBTYPE).ToString() = iTextSharp.text.pdf.PdfName.IMAGE.ToString()) Then
                    ' Grab various properties of the image.
                    Dim filter = pdfDict.Get(iTextSharp.text.pdf.PdfName.FILTER).ToString()
                    Dim width = pdfDict.Get(iTextSharp.text.pdf.PdfName.WIDTH).ToString()
                    Dim height = pdfDict.Get(iTextSharp.text.pdf.PdfName.HEIGHT).ToString()
                    Dim bpp = pdfDict.Get(iTextSharp.text.pdf.PdfName.BITSPERCOMPONENT).ToString()

                    ' Grab the raw bytes of the image
                    Dim bytes = PdfReader.GetStreamBytesRaw(DirectCast(obj, PRStream))

                    ' Images can be encoded in various ways. 
                    ' All of our images are encoded with a single filter.
                    ' If there is a need to decode another filter, it will need to be added.
                    If (filter = iTextSharp.text.pdf.PdfName.CCITTFAXDECODE.ToString()) Then
                        Using ms = New MemoryStream()
                            Using tiff As Tiff = tiff.ClientOpen("memory", "w", ms, New TiffStream())
                                tiff.SetField(TiffTag.IMAGEWIDTH, width)
                                tiff.SetField(TiffTag.IMAGELENGTH, height)
                                tiff.SetField(TiffTag.COMPRESSION, Compression.CCITTFAX4)
                                tiff.SetField(TiffTag.BITSPERSAMPLE, bpp)
                                tiff.SetField(TiffTag.SAMPLESPERPIXEL, 1)

                                tiff.WriteRawStrip(0, bytes, bytes.Length)
                                tiff.Flush()
                                images.Add(ms.ToArray())
                                tiff.Close()
                            End Using
                        End Using
                    Else
                        Throw New NotImplementedException("Decoding this filter has not been implemented")
                    End If
                End If
            End If
        Next
    End If

    Return images
End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接