之前使用过一款免费的pdf转图片的类库,但是免费的版本在pdf的页数上有一定的限制,后来在经过一番查找之后,找到了这个PDFtoImage,同样可以用来实现读取pdf文件并转换成为图片的功能。
首先,这里先通过NuGet安装PDFtoImage

之后使用下面的代码进行文件的转换
string fileName = "F:\\test\\1.pdf";
string savePath = "F:\\test\\images\\";
// 打开pdf文件所需要的密码
string password = null;
// PDF文件的页数
int count = 0;
using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
// 获取pdf文件的页码数
count = Conversion.GetPageCount(stream, password: password);
}
// 打开pdf文件
using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
// 渲染参数,设置在诸如渲染发票时,显示电子签章等,不设置渲染出的图片中会不包含这些内容
// 如果不需要可忽略此设置
RenderOptions options = new RenderOptions(
WithFormFill: true,
WithAnnotations: true
);
// 循环pdf页
for (int i = 0; i < count; i++)
{
using (FileStream outPugStream = File.OpenWrite(Path.Combine(savePath, $"({(i + 1)}).png")))
{
// 将当前页转换为png图片
Conversion.SavePng(outPugStream, stream, leaveOpen: true, password: password, page: new Index(i), options: options);
}
}
}
2854

被折叠的 条评论
为什么被折叠?



