使用 .NET Core 中的 Ghostscript 库可以将 PDF 转换为图像。

安装 Ghostscript 库:

  1. 在 Windows 上,下载 Ghostscript 安装程序(.exe 文件)并执行安装。
  2. 在 Linux 上,使用包管理器安装 Ghostscript。

使用 Ghostscript 库将 PDF 转换为图像:

using System.Diagnostics;

public void ConvertPdfToImage(string pdfPath, string imagePath)
{
    // Ghostscript 路径
    var ghostscriptPath = @"C:\Program Files\gs\gs9.54.0\bin\gswin64c.exe";

    // 调用 Ghostscript 命令行进行转换
    var process = new Process();
    var startInfo = new ProcessStartInfo
    {
        FileName = ghostscriptPath,
        Arguments = $"-dNOPAUSE -dBATCH -sDEVICE=png16m -r300 -sOutputFile={imagePath} {pdfPath}",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    };
    process.StartInfo = startInfo;
    process.Start();
    process.WaitForExit();
}

以上代码将 PDF 文件转换为 PNG 格式的图像文件,并将其保存在指定的位置。可以根据需要调整转换参数,例如更改输出图像格式或分辨率

C# net core 将 pdf 转换成 图片

原文地址: https://www.cveoy.top/t/topic/fEsc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录