c# - 临时将PDF转为图片,方便阅读二维码

标签 c# .net pdf qr-code

我需要能够从 PDF 文件中读取二维码。我正在使用 thoughtworks.QRCode,它接受图像并返回二维码中保存的数据。我有那个部分在工作。

但是,我需要能够接受多页 PDF 文件并将每页作为图像发送到 QR 阅读器。然后我需要将原始 PDF 的每一页保存为以二维码中包含的数据命名的单页 PDF。

您会推荐我在这个项目中使用什么库?我见过的许多作品都创造了永久的形象,但我只想要临时的。有什么东西可以让我轻松做到这一点吗?是否有另一个可以阅读 pdf 的 QR 阅读器?

感谢您提供的任何建议!

最佳答案

我使用 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

关于c# - 临时将PDF转为图片,方便阅读二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348444/

相关文章:

c# - This.Task() 到 VB 出错,如何解决?

c# - 在 C# 中,ToUpper() 和 ToUpperInvariant() 有什么区别?

c# - 将 FlowDocument 转换为 PDF 的最佳方式是什么

c++ - DLL 不适用于 x64 系统

google-chrome - Chrome 在从 PDF 复制并粘贴到 TinyMCE 的文本中添加了不间断空格

javascript - Php 运行 nodejs javascript grunt 任务

c# - 准备好的语句与带有 RefCursor 的存储过程

c# - 当 ID 自动递增时,如何在 Visual Studio 中将其他值插入数据库

c# - NuGet 包无法为 lib 目录中的 DLL 添加对项目的引用

c# - 在开发期间管理多个应用程序配置文件