简体   繁体   English

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

[英]Convert pdf into images using C# - Ghostscript

I need to convert pdf-files into images with a script in C# for Unity. 我需要使用C#for Unity中的脚本将pdf文件转换为图像。 Ghostscript shouldn't be installed on disk to do this. 不应在磁盘上安装Ghostscript来执行此操作。 I tried to include the ghostscript .dll and use it with a wrapper which uses the following functions: 我试图包括ghostscript .dll,并将其与使用以下功能的包装一起使用:

gsapi_new_instance
gsapi_init_with_args
gsapi_exit
gsapi_delete_instance

However, if I use the commands -sDEVICE=png16m -sOutputFile=outputpath.png inputpath.pdf as arguments, it is not working. 但是,如果我使用命令-sDEVICE=png16m -sOutputFile=outputpath.png inputpath.pdf作为参数,则它不起作用。 The first page of the pdf file is created but the others are not. pdf文件的首页已创建,而其他页面则没有。 I used "-%d" in the outputpath, but it's not working either. 我在输出路径中使用了“-%d”,但它也不起作用。 eg C:\\Example-%d.png 例如C:\\ Example-%d.png

I am using Ghostscript version 9.16, Unity version 5.2.0f3 and Visual Studio 2015. 我正在使用Ghostscript版本9.16,Unity版本5.2.0f3和Visual Studio 2015。

Can please anyone tell me, why the first image file is created and the others are not? 谁能告诉我,为什么创建第一个图像文件而其他人没有? Is there any simple alternative for using ghostscript to create multiple images from a pdf by using C#? 是否有任何简单的替代方法,可以使用Ghostscript通过C#从pdf创建多个图像?

Have you tried Magick.Net ? 您是否尝试过Magick.Net

It's a very popular .NET wrapper for the ImageMagick library (It uses Ghostscript under the hood for pdfs) 这是ImageMagick库的一个非常流行的.NET包装器(它在引擎盖下使用Ghostscript生成pdf)

First install NuGet Install-Package GhostScriptSharp . 首先安装NuGet Install-Package GhostScriptSharp Then you could do something like this: 然后,您可以执行以下操作:

/// <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.
    }

Many thanks to KenS and all the others. 非常感谢KenS和其他所有人。

My problem was that I defined -dNOPAUSE as my first argument. 我的问题是我将-dNOPAUSE定义为第一个参数。 However, the first argument shouldn't be defined for ghostscript. 但是,不应为ghostscript定义第一个参数。 It is not realized from ghostscript, so that's why I got only the first page. 它不是通过ghostscript实现的,所以这就是为什么我只有第一页的原因。 Thanks to KenS suggestion with -dNOPAUSE, I was able to find the error. 多亏了Kens建议-dNOPAUSE,我才能够找到错误。

I hope this helps others, too. 我希望这也会对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM