0
votes

I need to convert pdf-files into images with a script in C# for Unity. Ghostscript shouldn't be installed on disk to do this. I tried to include the ghostscript .dll and use it with a wrapper which uses the following functions:

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. The first page of the pdf file is created but the others are not. I used "-%d" in the outputpath, but it's not working either. e.g. C:\Example-%d.png

I am using Ghostscript version 9.16, Unity version 5.2.0f3 and 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#?

3
There's Ghostscript Sharp and Ghostscript.NET, neither are maintained by Artifex but by open source contributors. You don't specify which version of GS you are using but I would suggest that the reason you have a problem is because you have not specified -dNOPAUSE so GS is waiting for a keypress between pages. If you look at stdout you will probably see it asking for you to press return. - KenS
Hi, thanks for the fast answer. I am using version 9.16. I tested the application with -dNOPAUSE but it's still not working. - Tenshi
You probably need to look at the back channel output on stderr and stdout to see what is going on. Does the first page filename contain a numeric ? If not then you don't have the syntax correct for the output filename. - KenS
Yes, the first page contains a numeric. - Tenshi
Well, can't see why there would be a problem then, what does teh back channel contain ? - KenS

3 Answers

1
votes

Have you tried Magick.Net ?

It's a very popular .NET wrapper for the ImageMagick library (It uses Ghostscript under the hood for pdfs)

1
votes

First install 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.
    }
0
votes

Many thanks to KenS and all the others.

My problem was that I defined -dNOPAUSE as my first argument. However, the first argument shouldn't be defined for ghostscript. It is not realized from ghostscript, so that's why I got only the first page. Thanks to KenS suggestion with -dNOPAUSE, I was able to find the error.

I hope this helps others, too.