.NET 以PdfiumViewer方式PDF转为图片

创建项目

创建一个.net framework模板

image.png

下载依赖

  1. 安装 PdfiumViewer NuGet 包
  2. 安装 PdfiumViewer.Native.x86.v8-xfa NuGet 包
  3. 安装 PdfiumViewer.Native.x86_64.v8-xfa NuGet 包

image.png

目录结构

image.png

核心方法

PDFHelper

``` using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ConsoleApp9 { public static class PDFHelper { ///

/// pdf转图片 /// /// pdf路径 /// 输出图片路径 /// 输出图片名称 /// 输出图片后缀 /// 输出图片格式 /// 开始页码 /// 结束页码 public static void PdfToImage( string pdfPath, string imagePath, string imageName, string imagePathFormat, ImageFormat imageFormat, int startPageNum, int endPageNum ) { #region 文件夹及路径处理 if (!System.IO.Directory.Exists(imagePath)) { System.IO.Directory.CreateDirectory(imagePath); } if (!imagePath.EndsWith("\") && !imagePath.EndsWith("/")) { imagePath = imagePath + "\"; } if (!imagePathFormat.StartsWith(".")) { imagePathFormat = "." + imagePathFormat; } #endregion var pdf = PdfiumViewer.PdfDocument.Load(pdfPath);//读取pdf var pdfPage = pdf.PageCount;//pdf页码 var pdfSize = pdf.PageSizes; #region 开始结束页 if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdf.PageCount) { endPageNum = pdf.PageCount; } if (startPageNum > endPageNum)//开始>结束 { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } #endregion

for (int i = startPageNum; i <= endPageNum; i++)
            {
                System.Drawing.Size size = new System.Drawing.Size();
                //pdfSize为list类型,索引从0,而pdf页码从1开始,所以需要-1
                size.Width = (int)pdfSize[i - 1].Width;
                size.Height = (int)pdfSize[i - 1].Height;
                var stream = new System.IO.FileStream($"{imagePath}{imageName}-{i}{imagePathFormat}", System.IO.FileMode.Create);
                var image = pdf.Render(i - 1, size.Width, size.Height, 300, 300, PdfiumViewer.PdfRenderFlags.Annotations);
                image.Save(stream, imageFormat);
                stream.Close();
                image.Dispose();
                stream.Dispose();
                System.Diagnostics.Process.Start(imagePath);
            }
            pdf.Dispose();
        }
    }

}

```

测试

image.png

image.png

``` using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace ConsoleApp9 { internal class Program { static void Main(string[] args) { PDFHelper.PdfToImage( "Z:\ocr\tessdata\01.pdf", "Z:\ocr\tessdata\", "transed", "png", ImageFormat.Png, 1, 1 ); } } }

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值