.NET PDF转图片



VB.NET PDF转图片

该方法引用了第三方的dll

首先需要添加引用O2S.Components.PDFRender4NET.dll

  1. Imports O2S.Components.PDFRender4NET  
Imports O2S.Components.PDFRender4NET
  1. Public Enum Definition  
  2.         One = 1  
  3.         Two = 2  
  4.         Three = 3  
  5.         Four = 4  
  6.         Five = 5  
  7.         Six = 6  
  8.         Seven = 7  
  9.         Eight = 8  
  10.         Nine = 9  
  11.         Ten = 10  
  12. End Enum  
Public Enum Definition
        One = 1
        Two = 2
        Three = 3
        Four = 4
        Five = 5
        Six = 6
        Seven = 7
        Eight = 8
        Nine = 9
        Ten = 10
End Enum
  1. Sub ConvertPDF2Image(ByVal pdfInputPath As StringByVal imageOutputPath As StringByVal imageName As StringByVal startPageNum As IntegerByVal endPageNum As IntegerByVal imageFormat As ImageFormat, ByVal definition As Definition)  
  2.         '用O2S.Components.PDFRender4NET来完成PDF转图片  
  3.         Dim PDFFile As PDFFile = PDFFile.Open(pdfInputPath)  
  4.         If (Directory.Exists(imageOutputPath)) = False Then  
  5.             Directory.CreateDirectory(imageOutputPath)  
  6.         End If  
  7.         'validate pageNum  
  8.         If (startPageNum <= 0) Then  
  9.             startPageNum = 1  
  10.         End If  
  11.         If (endPageNum > PDFFile.PageCount) Then  
  12.             endPageNum = PDFFile.PageCount  
  13.         End If  
  14.         If (startPageNum > endPageNum) Then  
  15.             Dim tempPageNum As Integer = startPageNum  
  16.             startPageNum = endPageNum  
  17.             endPageNum = startPageNum  
  18.         End If  
  19.         ' start to convert each page  
  20.         For i = startPageNum To endPageNum  
  21.             Dim pageImage As Bitmap = PDFFile.GetPageImage(i - 1, 56 * Int(definition))  
  22.             pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat)  
  23.             pageImage.Dispose()  
  24.         Next  
  25.         PDFFile.Dispose()  
  26. End Sub  
Sub ConvertPDF2Image(ByVal pdfInputPath As String, ByVal imageOutputPath As String, ByVal imageName As String, ByVal startPageNum As Integer, ByVal endPageNum As Integer, ByVal imageFormat As ImageFormat, ByVal definition As Definition)
        '用O2S.Components.PDFRender4NET来完成PDF转图片
        Dim PDFFile As PDFFile = PDFFile.Open(pdfInputPath)
        If (Directory.Exists(imageOutputPath)) = False Then
            Directory.CreateDirectory(imageOutputPath)
        End If
        'validate pageNum
        If (startPageNum <= 0) Then
            startPageNum = 1
        End If
        If (endPageNum > PDFFile.PageCount) Then
            endPageNum = PDFFile.PageCount
        End If
        If (startPageNum > endPageNum) Then
            Dim tempPageNum As Integer = startPageNum
            startPageNum = endPageNum
            endPageNum = startPageNum
        End If
        ' start to convert each page
        For i = startPageNum To endPageNum
            Dim pageImage As Bitmap = PDFFile.GetPageImage(i - 1, 56 * Int(definition))
            pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat)
            pageImage.Dispose()
        Next
        PDFFile.Dispose()
End Sub


参数:

①pdfInputPath:PDF文件路径

②imageOutputPath:图片输出路径

③imageName:生成图片的名字

④startPageNum:从PDF文档的第几页开始转换

⑤endPageNum:从PDF文档的第几页开始停止转换

⑥imageFormat:设置所需图片格式

⑦definition:设置图片的清晰度,数字越大越清晰

也可以去掉这个参数,在函数中直接设置所需的dpi

  1. Dim pageImage As Bitmap = PDFFile.GetPageImage(i - 1, 200)  
Dim pageImage As Bitmap = PDFFile.GetPageImage(i - 1, 200)


如果需要O2S.Components.PDFRender4NET.dll,可以联系我~


http://blog.csdn.net/u013162930/article/details/47338081


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值