.net core 2.2 使用imagemagick 將pdf轉化為png


工作需要將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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM