PDFtoImage 将pdf文件转换为图片文件输出

之前使用过一款免费的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);
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值