【问题标题】:ghost4j class cast exception during joining two PostScripts加入两个 PostScript 期间的 ghost4j 类转换异常
【发布时间】:2013-12-07 06:52:35
【问题描述】:

我正在尝试将两个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");

在这个简化的过程中,我收到了上面“附加”行的以下异常消息:

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。

【问题讨论】:

  • ghost4j无法解析2.ps,可能是文档有错误,我说不出来。
  • 我认为解析发生在 load() 调用期间,而不是在 append 期间?
  • 顺便说一句。我在 GhostView 中打开两个 PosScript 文件都没有问题。

标签: java ghostscript postscript ghost4j


【解决方案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 可能是 sesponsable (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 没想到。

【讨论】:

    【解决方案2】:

    出现异常是因为 2 个文档之一不遵循 Adob​​e 文档结构约定 (DSC),如果您想使用 Documentappend 方法,这是强制性的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多