基于Aspose.Pdf把pdf文件每一页转换为图片

本文介绍了一个使用Aspose.Pdf库将PDF文件转换为JPG图片的方法。通过选择PDF文件并设置输出图片的质量,该方法可以将PDF文件的每一页转换为指定质量的JPG图片,并保存到指定目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,直接上代码片段

            //1.选择pdf文件
            var dialog = new OpenFileDialog();
            dialog.Filter = "pdf文件|*.pdf";
            var dialogResult = dialog.ShowDialog();
            if (dialogResult != System.Windows.Forms.DialogResult.OK) {
                return;
            }

            //和选择的文件并列创建一个目录
            string filePath = dialog.FileName;
            string directoryPath = filePath + "目录";
            //aspose许可证
            Aspose.Pdf.License l = new Aspose.Pdf.License();
            string licenseName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Aspose.Total.Product.Family.lic");
            l.SetLicense(licenseName);
            //定义Jpeg转换设备
            Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
            var device = new Aspose.Pdf.Devices.JpegDevice();
            int quality = int.Parse(this.comboBox1.SelectedItem.ToString());
            directoryPath += quality;
            Directory.CreateDirectory(directoryPath);
            //默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩
            device = new Aspose.Pdf.Devices.JpegDevice(quality);
            //遍历每一页转为jpg
            for (var i = 1; i <= document.Pages.Count; i++) {
                string filePathOutPut = Path.Combine(directoryPath, string.Format("{0}.jpg", i));
                FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);
                try {
                    device.Process(document.Pages[i], fs);
                    fs.Close();
                } catch (Exception ex) {
                    fs.Close();
                    File.Delete(filePathOutPut);
                }

            } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值