使用 C# - Ghostscript 将 pdf 转换为图像

Convert pdf into images using C# - Ghostscript

我需要使用 C# for Unity 中的脚本将 pdf 文件转换为图像。不应在磁盘上安装 Ghostscript 来执行此操作。我尝试包含 ghostscript .dll 并将其与使用以下函数的包装器一起使用:

gsapi_new_instance
gsapi_init_with_args
gsapi_exit
gsapi_delete_instance

但是,如果我使用命令 -sDEVICE=png16m -sOutputFile=outputpath.png inputpath.pdf 作为参数,它就不起作用。 pdf 文件的第一页已创建,但其他页面未创建。我在输出路径中使用了“-%d”,但它也不起作用。例如C:\例子-%d.png

我使用的是 Ghostscript 版本 9.16、Unity 版本 5.2.0f3 和 Visual Studio 2015。

谁能告诉我,为什么创建了第一个图像文件而其他的没有?使用 ghostscript 通过 C# 从 pdf 创建多个图像是否有任何简单的替代方法?

你试过了吗Magick.Net

它是 ImageMagick 库的一个非常流行的 .NET 包装器 (它在 pdf 的底层使用 Ghostscript)

首先安装 NuGet Install-Package GhostScriptSharp。 然后你可以这样做:

/// <summary>
/// Convertetion PDF to image.
/// </summary>
/// <param name="Path">Path to file for convertetion</param>
/// <param name="PDFfile">Book name on HDD</param>
/// <param name="Devise">Select one of the formats, jpg</param>
/// <param name="PageFormat">Select one of page formats, like A4</param>
/// <param name="qualityX"> Select quality, 200X200 ~ 1200X1900</param>
/// <param name="qualityY">Select quality, 200X200 ~ 1200X1900</param>
    public void CreatPDF(string Path, string PDFfile, GhostscriptSharp.Settings.GhostscriptDevices Devise,
GhostscriptSharp.Settings.GhostscriptPageSizes PageFormat, int qualityX, int qualityY)
    {
        GhostscriptSharp.GhostscriptSettings SettingsForConvert = new GhostscriptSharp.GhostscriptSettings();

        SettingsForConvert.Device = Devise;

        GhostscriptSharp.Settings.GhostscriptPageSize pageSize = new GhostscriptSharp.Settings.GhostscriptPageSize();
        pageSize.Native = PageFormat;
        SettingsForConvert.Size = pageSize;

        SettingsForConvert.Resolution = new System.Drawing.Size(qualityX, qualityY);

        GhostscriptSharp.GhostscriptWrapper.GenerateOutput(Path, @"C:\" + PDFfile + "\" + PDFfile + "_" + "%d.jpg", SettingsForConvert); // here you could set path and name for out put file.
    }

非常感谢 KenS 和所有其他人。

我的问题是我将 -dNOPAUSE 定义为我的第一个参数。但是,不应为 ghostscript 定义第一个参数。它不是从 ghostscript 实现的,所以这就是为什么我只得到第一页的原因。感谢 KenS 对 -dNOPAUSE 的建议,我能够找到错误。

我希望这对其他人也有帮助。