工作需要将PDF文件每一页拆分为一个一个的png文件

测试环境:mac,visual studio for mac 2019

nuget:magick.net-Q16-AnyCPU

不能直接支持PDF

需要安装一个包:ghostscript

在命令行下 brew install ghostscript

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

12-16 06:21
查看更多