0

当我运行此代码时:

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。一切似乎都是正确的,但它很简单是行不通的。

4

2 回答 2

1

-15 是“范围检查”错误。应该有相当多的额外反向通道信息,这些信息可能会提供一些有用的细节。但是,由于您没有直接使用 Ghostscript,因此我无法告诉您它的去向。

您至少应该将您用作输入的 PDF 文件放在公开的地方,以便我们查看它。

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

恐怕任何人都无能为力,你让我们继续做下去。

于 2015-07-07T07:07:41.273 回答
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();
}

谢谢大家。

于 2015-07-08T20:43:40.710 回答