ghost4j类在加入两个PostScripts时抛出异常

时间:2013-11-22 11:41:02

标签: java ghostscript postscript ghost4j

我正在尝试将两个PostScript文件加入到ghost4j 0.5.0的文件中,如下所示:

final PSDocument[] psDocuments = new PSDocument[2];
psDocuments[0] = new PSDocument();
psDocuments[0].load("1.ps");
psDocuments[1] = new PSDocument();
psDocuments[1].load("2.ps");
psDocuments[0].append(psDocuments[1]);
psDocuments[0].write("3.ps");

在此简化过程中,我收到以下“append”行的异常消息:

org.ghost4j.document.DocumentException: java.lang.ClassCastException:   
org.apache.xmlgraphics.ps.dsc.events.UnparsedDSCComment cannot be cast to 
org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage

到目前为止,我还没有弄清楚这里的问题是什么 - 可能是其中一个PostScript文件中的某种问题?

所以请帮助。

修改

我使用ghostScript命令行工具测试:

gswin32.exe -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pswrite -sOutputFile="test.ps" --filename "1.ps" "2.ps"

导致文档中1.ps和2.ps合并为一个(!)页面(即叠加)。 删除--filename时,生成的文档将是一个具有两个页面的PostScript。

2 个答案:

答案 0 :(得分:1)

我认为文档或XMLGraphics库中存在错误,因为它似乎无法解析其中的一部分。

在这里你可以看到ghost4j中我认为它失败的代码(link):

    DSCParser parser = new DSCParser(bais);
    Object tP = parser.nextDSCComment(DSCConstants.PAGES);
    while (tP instanceof DSCAtend)
        tP = parser.nextDSCComment(DSCConstants.PAGES);
    DSCCommentPages pages = (DSCCommentPages) tP;

在这里,你可以看到为什么XMLGraphics可以支配责任(link):

private DSCComment parseDSCComment(String name, String value) {
    DSCComment parsed = DSCCommentFactory.createDSCCommentFor(name);
    if (parsed != null) {
        try {
            parsed.parseValue(value);
            return parsed;
        } catch (Exception e) {
            //ignore and fall back to unparsed DSC comment
        }
    }
    UnparsedDSCComment unparsed = new UnparsedDSCComment(name);
    unparsed.parseValue(value);
    return unparsed;
}

似乎parsed.parseValue(value)抛出了异常,它隐藏在catch中,它返回了ghost4j没想到的未解析版本。

答案 1 :(得分:1)

发生异常是因为其中一个文档不遵循Adobe文档结构公约(DSC),如果您想使用Document append方法,则必须使用此文档。

请改用SafeAppenderModifier。这里有一个例子:http://www.ghost4j.org/highlevelapisamples.html(将PDF文档附加到PostScript文档)