GhostScript .NET not continuing past certain pages

374 Views Asked by At

I've created a program which needs to convert PDF files into image files, and for this GhostScript is the best choice. But once in a while, the library stalls completely on a page and doesn't continue, it just keeps using CPU power and working, as though it might be caught in an infinite loop. The error is easily reproduce-able as it happens every time on the specific PDF files that it occurs on, though no error is given from GhostScript of any kind, and nothing is out of the ordinary in the PDF files themselves as far as I can see.

I have however been able to find out that the stalling is due to a specific element or elements in the pdf files, and by deleting the elements the pdf will easily render in GhostScript, but this is not a solution, nor an answer I can use.

PDF link* - http://www.filedropper.com/usjunis1-32webtest

*saved with free version of PDF-XChange Editor, so it has watermarks at the top, but it is the square that creates the stalling. I've also seen it happen on vector graphics objects, so it is not limited to squares.

Code -

private void startImageProcessing(String pdfFile)
{
    GhostscriptVersionInfo gvi = new GhostscriptVersionInfo(new Version(0, 0, 0), Directory.GetCurrentDirectory() + @"\gsdll32.dll", string.Empty, GhostscriptLicense.GPL);
    Ghostscript.NET.Processor.GhostscriptProcessor processor = new Ghostscript.NET.Processor.GhostscriptProcessor(gvi, true);
    processor.StartProcessing(CreateTestArgs(pdfFile, pdfFile.Substring(0, pdfFile.Length - 4) + "\\"+prefix+"-%03d.jpg", 72 * scale), new ConsoleStdIO(true));
}

private static string[] CreateTestArgs(string inputPath, string outputPath, int dpi)
{
    List<string> gsArgs = new List<string>();
    gsArgs.Add("-dSAFER");
    gsArgs.Add("-dBATCH");
    gsArgs.Add("-dNOPAUSE");
    gsArgs.Add("-sDEVICE=jpeg");
    gsArgs.Add("-r" + dpi);
    gsArgs.Add("-dJPEGQ=100");
    gsArgs.Add("-dNumRenderingThreads=" + Environment.ProcessorCount.ToString());
    gsArgs.Add("-dTextAlphaBits=4");
    gsArgs.Add("-dGraphicsAlphaBits=4");
    gsArgs.Add(@"-sOutputFile=" + outputPath);
    gsArgs.Add(@"-f" + inputPath);
    return gsArgs.ToArray();
}

I've also created a pdf file only containing one of the wrong elements for testing, and it has both had the error when saved by Adobe Acrobat, and PDF-XChange Editor, so the error is not due to a specific program that I've used to save the PDF either.

0

There are 0 best solutions below