ghostscript.net rasterizer'gsapi_init_with_args': - 15错误

时间:2015-07-07 01:56:25

标签: c# ghostscript ghostscript.net

当我运行此代码时:

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

我收到此错误

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

错误在这一行:     rasterizer.Open(stream,latestVersion,false);

任何人都可以指出我造成这种情况的原因是什么?

我在本地机器上运行它。在软件包管理器控制台上安装了Ghostscript。一切似乎都是正确的,但它很简单无效。

2 个答案:

答案 0 :(得分:1)

-15是一个'范围检查'错误。应该有相当多的额外反向信道信息可能会提供一些有用的细节。但是,由于你没有直接使用Ghostscript,我无法告诉你它可能会发生什么。

您应该将您正在使用的PDF文件作为输入放在公共场所,至少我们可以查看它。

理想情况下,您应该从命令行重现Ghostscript本身的问题,但无论如何您必须提供配置信息(即您使用的设置)。 Ghostscript的版本(以及它的32或64位)也是有用的信息。

我担心没有人会对你给我们继续做的事做些什么。

答案 1 :(得分:1)

这是我的工作范例。

所以我调用方法ResizePDF(string filePath)并将包含扩展名的文件路径(例如C:\ tmp \ file.pdf)作为参数。

该方法返回memoryStream,其中包含可用于执行任何操作的已调整大小的文件。

围绕它有一些工作要做,但到目前为止它仍然有效。

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();
}

谢谢大家。