1

When I run this code:

var stream = File.OpenRead(@"C:\tmp\PdfToTest.PDF");
var latestVersion = GhostscriptVersionInfo.GetLastInstalledVersion();
rasterizer = new GhostscriptRasterizer();
rasterizer.Open(stream, latestVersion, false);

I am getting this error

An exception of type 'Ghostscript.NET.GhostscriptAPICallException' occurred in Ghostscript.NET.dll but was not handled in user code

Additional information: An error occured when call to 'gsapi_init_with_args' is made: -15

The error is in this line: rasterizer.Open(stream, latestVersion, false);

Anyone could point me what it is causing this to happen?

I am running this in local machine. Installed the Ghostscript on Package manager console. Everything seems to be right, but it simple doesn't work.

KleberBH
  • 452
  • 1
  • 9
  • 28

2 Answers2

1

-15 is a 'rangecheck' error. There should be considerable extra backchannel information which might give some useful details. However since you are not using Ghostscript directly I can't tell you where it might be going.

You should put the PDF file you are using as input somewhere public at least so we can look at it.

Ideally you should reproduce the problem with Ghostscript itself, from the command line, but in any event you must supply the configuration information (ie what settings you have used). The version of Ghostscript (and whether its 32 or 64 bit) would also be useful information.

I'm afraid there's nothing much anyone can do with what you've given us to go on.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • I ran this on the command prompt: 'gswin32c -dQUIET -dBATCH -dNOPAUSE -dSAFER -dMaxSubsetPct=100 -dPDFSETTINGS=/ebook -sDEVICE=pdfwrite -sOutputFile=c:\tmp\out.pdf c:\tmp\PdfToTest.pdf' and it created the pdf exactly as I expected. But on my c# code it still not working. I am running the 32 bit version. – KleberBH Jul 07 '15 at 20:59
  • Well if works from the command line, then its not Ghostscript or your PDF file at fault. It could be something odd about the way the .NET wrapper works. I'm afraid that's not part of the Ghostscript distribution so I can't help there, you need jhabjan to look into it. – KenS Jul 08 '15 at 07:10
  • @KleberBH: The problem is that you are using GhostscriptRasterizer which converts PDF to Image object and not back to PDF. What you are looking for is GhostscriptProcessor. Take look at this example: https://github.com/jhabjan/Ghostscript.NET/blob/master/Ghostscript.NET.Samples/Samples/ProcessorSample1.cs – HABJAN Jul 08 '15 at 09:23
  • 1
    Thanks guys for all the replies, I got it working. As it is too big to post here, I will post as an answer, so it may help someone else. – KleberBH Jul 08 '15 at 20:27
1

This is my working example.

So I call the method ResizePDF(string filePath) and give the file path including extension (eg. C:\tmp\file.pdf) as parameter.

The method returns the memoryStream with the resized file that I can use to do whatever.

There are some work to do around it, however it is working so far.

internal MemoryStream ResizePDF(string filePath)
{
    string inputFilePath = String.Format(@"{0}", filePath);
    GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();
    string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");

    MemoryStream memStream = null;
    using (GhostscriptProcessor processor = new GhostscriptProcessor())
    {
        try
        {
            processor.Process(GetGsArgs(inputFile, outputPipeHandle));
            byte[] rawDocumentData = gsPipedOutput.Data;
            memStream = new MemoryStream(rawDocumentData);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            gsPipedOutput.Dispose();
            gsPipedOutput = null;
        }
    }
    return memStream;
}

private string[] GetGsArgs(string inputFilePath, string outputFilePath)
{
        List<string> switches = new List<string>();
        switches.Add("-empty");
        switches.Add("-dQUIET");
        switches.Add("-dSAFER");
        switches.Add("-dBATCH");
        switches.Add("-dNOPAUSE");
        switches.Add("-dNOPROMPT");
        switches.Add("-dPDFSETTINGS=/ebook");
        switches.Add("-sDEVICE=pdfwrite");
        switches.Add("-sPAPERSIZE=a4");
        switches.Add("-sOutputFile=" + outputPipeHandle);
        switches.Add("-f");
        switches.Add(inputFilePath);
        return switches.ToArray();
}

Thanks you all.

KleberBH
  • 452
  • 1
  • 9
  • 28