![](/img/trans.png)
[英]while creating pdf new page get first page data using itextsharp c#
[英]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
您是在Web還是本機環境中? 它制造了巨大的差異。 您想要將PDF光柵化為圖像。 這很容易通過GhostDoc或類似工具在本機環境中完成。 他們都使用虛擬打印機驅動程序來光柵化PDF。 這種方法在網絡環境中行不通,因為您可能需要使用商業用途,因為編寫自己的光柵化引擎是一項艱巨的任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.