Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pdf As New PdfReader("C:/1.pdf")
Dim N As Int16
Try
Dim numberOfPages As Integer = pdf.NumberOfPages
Dim pageNumber As Integer = 1
While pageNumber <= numberOfPages
Dim pg As PdfDictionary = pdf.GetPageN(pageNumber)
Dim img As System.Drawing.Image = GetImage(pg, pdf)
If img IsNot Nothing Then
N = N + 1
img.Save("C:/" & N & ".bmp", System.Drawing.Imaging.ImageFormat.Bmp)
End If
System.Math.Max(System.Threading.Interlocked.Increment(pageNumber), pageNumber - 1)
End While
Catch
Throw
Finally
pdf.Close()
End Try
End Sub
Private Shared Function GetImage(ByVal dict As PdfDictionary, ByRef doc As PdfReader) As System.Drawing.Image
Dim res As PdfDictionary = CType(PdfReader.GetPdfObject(dict.[Get](PdfName.RESOURCES)), PdfDictionary)
Dim xobj As PdfDictionary = CType(PdfReader.GetPdfObject(res.[Get](PdfName.XOBJECT)), PdfDictionary)
If xobj IsNot Nothing Then
Dim width As Integer = 0, height As Integer = 0
Dim bmp As New Bitmap(2048, 2048)
Dim g As Graphics = Graphics.FromImage(bmp)
For Each name As PdfName In xobj.Keys
Dim obj As PdfObject = xobj.[Get](name)
If obj.IsIndirect() Then
Dim tg As PdfDictionary = CType(PdfReader.GetPdfObject(obj), PdfDictionary)
Dim type As PdfName = CType(PdfReader.GetPdfObject(tg.[Get](PdfName.SUBTYPE)), PdfName)
If PdfName.IMAGE.Equals(type) Then
Dim XrefIndex As Integer = Convert.ToInt32(CType(obj, PRIndirectReference).Number.ToString(System.Globalization.CultureInfo.InvariantCulture))
Dim pdfObj As PdfObject = doc.GetPdfObject(XrefIndex)
Dim pdfStrem As PdfStream = CType(pdfObj, PdfStream)
Dim bytes As Byte() = PdfReader.GetStreamBytesRaw(CType(pdfStrem, PRStream))
If bytes Is Nothing Then
Continue For
End If
Using memory As New System.IO.MemoryStream(bytes)
Try
memory.Position = 0
Dim tmp As New Bitmap(System.Drawing.Image.FromStream(memory))
width = Math.Max(tmp.Width, width)
g.DrawImage(tmp, 0, height)
height = height + tmp.Height
Catch
End Try
End Using
End If
End If
Next
Dim cloneRect As New RectangleF(0, 0, width, height)
Dim format As Imaging.PixelFormat = bmp.PixelFormat
Dim cloneBitmap As Bitmap = bmp.Clone(cloneRect, format)
bmp.Dispose()
Return cloneBitmap
End If
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pdf As New PdfReader("C:/1.pdf")
Dim N As Int16
Try
Dim numberOfPages As Integer = pdf.NumberOfPages
Dim pageNumber As Integer = 1
While pageNumber <= numberOfPages
Dim pg As PdfDictionary = pdf.GetPageN(pageNumber)
Dim img As System.Drawing.Image = GetImage(pg, pdf)
If img IsNot Nothing Then
N = N + 1
img.Save("C:/" & N & ".bmp", System.Drawing.Imaging.ImageFormat.Bmp)
End If
System.Math.Max(System.Threading.Interlocked.Increment(pageNumber), pageNumber - 1)
End While
Catch
Throw
Finally
pdf.Close()
End Try
End Sub
Private Shared Function GetImage(ByVal dict As PdfDictionary, ByRef doc As PdfReader) As System.Drawing.Image
Dim res As PdfDictionary = CType(PdfReader.GetPdfObject(dict.[Get](PdfName.RESOURCES)), PdfDictionary)
Dim xobj As PdfDictionary = CType(PdfReader.GetPdfObject(res.[Get](PdfName.XOBJECT)), PdfDictionary)
If xobj IsNot Nothing Then
Dim width As Integer = 0, height As Integer = 0
Dim bmp As New Bitmap(2048, 2048)
Dim g As Graphics = Graphics.FromImage(bmp)
For Each name As PdfName In xobj.Keys
Dim obj As PdfObject = xobj.[Get](name)
If obj.IsIndirect() Then
Dim tg As PdfDictionary = CType(PdfReader.GetPdfObject(obj), PdfDictionary)
Dim type As PdfName = CType(PdfReader.GetPdfObject(tg.[Get](PdfName.SUBTYPE)), PdfName)
If PdfName.IMAGE.Equals(type) Then
Dim XrefIndex As Integer = Convert.ToInt32(CType(obj, PRIndirectReference).Number.ToString(System.Globalization.CultureInfo.InvariantCulture))
Dim pdfObj As PdfObject = doc.GetPdfObject(XrefIndex)
Dim pdfStrem As PdfStream = CType(pdfObj, PdfStream)
Dim bytes As Byte() = PdfReader.GetStreamBytesRaw(CType(pdfStrem, PRStream))
If bytes Is Nothing Then
Continue For
End If
Using memory As New System.IO.MemoryStream(bytes)
Try
memory.Position = 0
Dim tmp As New Bitmap(System.Drawing.Image.FromStream(memory))
width = Math.Max(tmp.Width, width)
g.DrawImage(tmp, 0, height)
height = height + tmp.Height
Catch
End Try
End Using
End If
End If
Next
Dim cloneRect As New RectangleF(0, 0, width, height)
Dim format As Imaging.PixelFormat = bmp.PixelFormat
Dim cloneBitmap As Bitmap = bmp.Clone(cloneRect, format)
bmp.Dispose()
Return cloneBitmap
End If
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
【iTextSharp导出pdf中的图片.rar】点击下载此文件
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |