工作需要将 PDF 文件每一页拆分为一个一个的 PNG 文件
测试环境: Mac,visual studio for Mac 2019
nuget:magick.NET-Q16-AnyCPU
不能直接支持 PDF, 还需要安装一个包: ghostscript, 进行下面步骤
1. 打开终端
2. 在命令行下 brew installghostscript, 等待安装完成
3. 开始写代码
- using System;
- using System.IO;
- using ImageMagick;
- namespace pngTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("derek-moxlink");
- string path = @"/Users/zuibangbang/Desktop/123.pdf";
- MagickReadSettings settings = new MagickReadSettings();
- settings.Density = new Density(300, 300); // 设置质量
- using (MagickImageCollection images = new MagickImageCollection())
- {
- try
- {
- images.Read(path, settings);
- for (int i = 0; i < images.Count; i++)
- {
- MagickImage image = (MagickImage)images[i];
- image.Format = MagickFormat.PNG;
- image.Write(path.Replace(Path.GetExtension(path), "") +"-"+ i +".png");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- Console.ReadKey();
- }
- }
- }
代码运行后, 将 PDF 拆分成 -0.PNG,-1.PNG,-2.PNG.....
本方法也可以拆分 tiff, 也可以进行其他图片格式的相互转换
工作记录 lxp
来源: http://www.bubuko.com/infodetail-3332351.html