有没有什么办法让c#中的pdf文件第一页的图像?

问题描述:

我正在寻找一种方法来获取使用c#的pdf文件中的第一页图像# 任何解决方案?有没有什么办法让c#中的pdf文件第一页的图像?

+3

到目前为止你做了什么? – 2012-03-23 17:18:29

+0

我在codeproject.com找到一篇文章,但看起来很复杂..我需要一些库,这很容易做到这一点 – dhyabi 2012-03-23 17:22:16

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 
+1

当你重定向到其他网页(特别是线程)时要小心,因为它们可能会在某个时候被删除,你的答案将是无意义。你可以在回答中加入一些代码,并把链接作为参考:) – 2012-03-23 17:24:35

+0

tyvm huMpty的提示! – 2012-03-23 19:29:22

+0

什么是我的意思是“对于我整数= 0到reader.XrefSize - 1” – dhyabi 2012-03-23 19:37:18

你大概栅格化PDF的网页复制的代码。如果查找获取图像等,您将发现可以在PDF上执行的其他操作。有已发布方式的list。我用ABCpdf很容易做到这一点。

您是否处于网络环境或本地环境?它制造了巨大的差异。你想要的是将PDF光栅化成图像。这很容易通过GhostDoc或类似工具在本地环境中执行。他们都使用虚拟打印机驱动程序来光栅化PDF。这种方法在Web环境中不适用,在这种环境中,您可能需要使用某些商业产品,因为编写自己的栅格化引擎是一项艰巨的任务。

+0

我的意思是动态获取图像在asp.net中的c#程序或网页的运行时 – dhyabi 2012-03-23 18:20:46