[英]Is there any way to get the image of the first page of pdf file in c#?
我正在寻找一种使用c#获取pdf文件中第一页图像的方法任何解决方案??
iTextSharp should handle that. iTextSharp应该可以解决这个问题。 Exit on the first image
在第一个图像上退出
example here http://www.vbforums.com/showthread.php?t=530736 此处的示例http://www.vbforums.com/showthread.php?t=530736
Edit: 编辑:
Copied code from the thread by stanav 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
You are probably trying to rasterize the pages of the PDF. 您可能正在尝试光栅化PDF页面。 If look for get image etc you will turn up other operations that you could perform on a PDF.
如果寻找图像等,您将打开其他可以在PDF上执行的操作。 There are a list of ways already posted.
有已经发布的方法列表 。 I've used ABCpdf to do this very easily.
我使用ABCpdf可以很容易地做到这一点。
Are you in a web, or native environment? 您是在Web还是本机环境中? It makes a huge difference.
它制造了巨大的差异。 What you want to is rasterize the PDF into an image.
您想要将PDF光栅化为图像。 This is easy enough to do in a native environment via GhostDoc or a similar tool.
这很容易通过GhostDoc或类似工具在本机环境中完成。 They all use a virtual printer driver to rasterize the PDF.
他们都使用虚拟打印机驱动程序来光栅化PDF。 This approach won't work in a web-environment where you will probably need to use something commercial as writing your own rasterizing engine is a massive undertaking.
这种方法在网络环境中行不通,因为您可能需要使用商业用途,因为编写自己的光栅化引擎是一项艰巨的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.