有没有办法在c#中获得pdf文件第一页的图像

本文关键字:文件 pdf 第一页 图像 有没有 | 更新日期: 2023-09-27 18:28:54

Am正在寻找一种使用c#获取pdf文件中第一页图像的方法有解决方案吗??

有没有办法在c#中获得pdf文件第一页的图像

iTextSharp应该处理这个问题。在第一个图像上退出

此处的示例http://www.vbforums.com/showthread.php?t=530736

编辑:

通过stanav 从线程复制代码

Public Shared Function ExtractImages(ByVal sourcePdf As String) As List(Of Image)
    Dim imgList As New List(Of Image)
    Dim raf As iTextSharp.text.pdf.RandomAccessFileOrArray = Nothing
    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    Dim pdfObj As iTextSharp.text.pdf.PdfObject = Nothing
    Dim pdfStrem As iTextSharp.text.pdf.PdfStream = Nothing
    Try
        raf = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
        reader = New iTextSharp.text.pdf.PdfReader(raf, Nothing)
        For i As Integer = 0 To reader.XrefSize - 1
            pdfObj = reader.GetPdfObject(i)
            If Not IsNothing(pdfObj) AndAlso pdfObj.IsStream() Then
                pdfStrem = DirectCast(pdfObj, iTextSharp.text.pdf.PdfStream)
                Dim subtype As iTextSharp.text.pdf.PdfObject = pdfStrem.Get(iTextSharp.text.pdf.PdfName.SUBTYPE)
                If Not IsNothing(subtype) AndAlso subtype.ToString = iTextSharp.text.pdf.PdfName.IMAGE.ToString Then
                    Dim bytes() As Byte = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw(CType(pdfStrem, iTextSharp.text.pdf.PRStream))
                    If Not IsNothing(bytes) Then
                        Try
                            Using memStream As New System.IO.MemoryStream(bytes)
                                memStream.Position = 0
                                Dim img As Image = Image.FromStream(memStream)
                                imgList.Add(img)
                            End Using
                        Catch ex As Exception
                            'Most likely the image is in an unsupported format
                            'Do nothing
                            'You can add your own code to handle this exception if you want to
                        End Try
                    End If
                End If
            End If
        Next
        reader.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Return imgList
End Function 

您可能正在尝试光栅化PDF的页面。如果寻找获取图像等,你会打开其他操作,你可以在PDF上执行。已经发布了一个方法列表。我用ABCpdf很容易做到这一点。

您是在web环境中还是在本机环境中?它带来了巨大的不同。您想要的是将PDF光栅化为图像。这在本机环境中通过GhostDoc或类似工具很容易做到。他们都使用虚拟打印机驱动程序来光栅化PDF。这种方法在网络环境中不起作用,因为编写自己的光栅化引擎是一项艰巨的任务,因此您可能需要使用商业化的东西。