【问题标题】:PDFBOX - when signing PDF, SignatureInterface's sign is not calledPDFBOX - 签署 PDF 时,不调用 SignatureInterface 的签名
【发布时间】:2019-12-17 19:38:54
【问题描述】:

我正在尝试使用 PDFBOX 库签署 PDF(主要关注 CreateSignature 和 GitHub 中 example 包中的其他类)。

采用SignatureInterface 方法后,我将其提供给addSignature,但sign 方法未被调用

相关代码sn-p:

private void signPDF(PDDocument document) throws Exception {
    PDSignature signature = new PDSignature();
    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    signature.setName("iSure");
    signature.setLocation("IL");
    signature.setReason("Security");
    signature.setSignDate(Calendar.getInstance(TimeZone.getTimeZone("UTC")));

    // this is the only 'foreign' call here, it looks exactly as in example
    setMDPPermission(document, signature, 1);

    document.addSignature(signature, new Signer());
}

在调用signPDF 之后,文档立即被保存到OutputStream

PS:在 MDP 权限中,我需要删除“DigestMethod”条目,它未能通过 PDF/A 元数据验证 - 对吗?

【问题讨论】:

  • 签名是在增量保存期间创建的。请按照Create*Signature* 示例进行操作,直至并包括该保存步骤。
  • 是的,我自己在调试COSWriter 时发现了它,并在工作代码下方发布了(有一些设计问题:)),无论如何谢谢!

标签: pdf pdfbox sign


【解决方案1】:

嗯,跟踪COSWriter流程,我终于发现,当保存不是增量时,根本没有调用写签名方法。这部分在示例中已明确说明,但我认为它仅与那些特定场景或继续使用 PDDocument 相关,而我的情况是签名实际上是流程中的终止操作。底线 - 我的坏:(

所以我把上面的方法改成如下:

// since forced to saveIncremental, need to finilized the input document and return a new one
private PDDocument signPDF(PDDocument document) throws Exception {
    PDSignature signature = new PDSignature();
    ... this part stays exactly as it was
    document.addSignature(signature, new Signer());

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        document.saveIncremental(baos);
        return PDDocument.load(baos.toByteArray());
    }
}

另一种可能性是将目标OutputStream 传递给signPDF 方法并在此处执行实际的流程终止:保存文档并随后关闭它。

另一种方法是在主流程中调用saveIncrementalsignPDF 调用者)。

在我看来,所有这些变化就像在方法之间滚动外国问题一样......如果有人有更好的方法来说明 - 我会很高兴听到。

【讨论】:

  • 我建议您不要返回PDDocument,而是返回字节数组。如果调用者不恰当地保存了PDDocument,他的签名将会损坏。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 2015-05-27
  • 2015-12-29
  • 2015-02-11
相关资源
最近更新 更多