分享

.net core 2.2 使用imagemagick 将pdf转化为png(示例代码)

 F2967527 2022-08-16 发布于北京

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

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

nuget:magick.net-Q16-AnyCPU

不能直接支持PDF,还需要安装一个包:ghostscript,进行下面步骤

1.打开终端

2.在命令行下 brew install ghostscript,等待安装完成

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多