繁体   English   中英

有什么方法可以在c#中获取pdf文件首页的图像吗?

[英]Is there any way to get the image of the first page of pdf file in c#?

我正在寻找一种使用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。 这种方法在网络环境中行不通,因为您可能需要使用商业用途,因为编写自己的光栅化引擎是一项艰巨的任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM