【问题标题】:Convert pdf into images using C# - Ghostscript使用 C# 将 pdf 转换为图像 - Ghostscript
【发布时间】:2015-12-14 10:10:02
【问题描述】:

我需要使用 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:\Example-%d.png

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

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

【问题讨论】:

  • 有 Ghostscript Sharp 和 Ghostscript.NET,它们都不是由 Artifex 维护的,而是由开源贡献者维护的。您没有指定您使用的是哪个版本的 GS,但我建议您遇到问题的原因是因为您没有指定 -dNOPAUSE 所以 GS 正在等待页面之间的按键。如果您查看标准输出,您可能会看到它要求您按回车键。
  • 您好,感谢您的快速答复。我使用的是 9.16 版。我用 -dNOPAUSE 测试了应用程序,但它仍然无法正常工作。
  • 您可能需要查看 stderr 和 stdout 上的反向通道输出以了解发生了什么。第一页文件名是否包含数字?如果不是,那么您的输出文件名的语法不正确。
  • 是的,第一页包含一个数字。
  • 好吧,不明白为什么会有问题,反向通道包含什么?

标签: c# pdf unity3d png ghostscript


【解决方案1】:

你试过Magick.Net吗?

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

【讨论】:

  • 您好,感谢您的快速答复。我会试试看。感谢您的建议。
【解决方案2】:

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

【讨论】:

  • 您好,感谢您的快速答复!我会试试看!
  • 如果我尝试在 Visual Studio 2015 中安装该软件包,我会收到以下错误消息:install-package : Could not install package 'Ghostscript.NET 1.2.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v3.5', but the package does not contain any assembly references or content files that are compatible with that framework. 抱歉这个愚蠢的问题,但有什么解决方法的想法吗?
  • 您可以在这里ghostscriptnet.codeplex.com/releases/view/611369 获取它,然后添加到您的解决方案中。
  • 感谢您的链接!我将 Ghostscript.NET.dll 添加到我的项目引用中。如何在您的代码中使用 GhostscriptSharp?
  • 好的,我修正了我的答案。看看它。这就像我在visual studio 12中检查过的那样工作。包工作正常。
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多