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

时间:2015-09-17 13:51:01

标签: c# pdf unity3d png ghostscript

我需要使用C#中的脚本将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"在outputpath中,但它也没有工作。例如C:\例 - ​​%d.png

我正在使用Ghostscript版本9.16,Unity版本5.2.0f3和Visual Studio 2015。

可以请任何人告诉我,为什么第一个图像文件被创建而其他图像文件没有?有没有使用ghostscript通过使用C#创建多个图像的简单替代方法?

3 个答案:

答案 0 :(得分:1)

您是否尝试过Magick.Net

它是ImageMagick库的一个非常流行的.NET包装器 (它使用引擎盖下的Ghostscript来获取pdfs)

答案 1 :(得分:0)

首先安装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.
    }

答案 2 :(得分:0)

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

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

我希望这也有助于其他人。

相关问题