簡體   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