将附加页面添加到已签名的 pdf 并再次签名

Adding additional page to signed pdf and sign it again

是否可以在已签名的 PDF 中添加额外的页面并在不破坏第一个签名的情况下再次签名。

我在增量更新下阅读了in the adobe documentation,这可能是可能的。

但是,我不确定这是否适用于所有内容或仅适用于注释(评论)、表单填写和数字签名。

我尝试通过在 Java 中使用 Apache PDFBox 来完成此操作,方法是签署文档,然后加载它,将页面附加到它,使用 saveIncremental() 保存它并再次签名。

但是,第一个签名失效了。

这是生成新 PDF 的 generateTest 方法:

public byte[] generateTest(InputStream requestPdfIn) throws IOException {

    // Create a document and add a page to it
    // PDDocument document = new PDDocument();
    PDDocument document = PDDocument.load(requestPdfIn);
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);     

    COSBase item = document.getPages().getCOSObject().getItem(COSName.KIDS);
    ((COSUpdateInfo) item).setNeedToBeUpdated(true);
    COSArray kids = (COSArray) item;
    kids.setNeedToBeUpdated(true);
    ((COSUpdateInfo) kids.get(0)).setNeedToBeUpdated(true);

    document.getPage(0).getCOSObject().setNeedToBeUpdated(true);
    page.getCOSObject().setNeedToBeUpdated(true);
    document.getPages().getCOSObject().setNeedToBeUpdated(true);

    COSDictionary dict = page.getCOSObject();
    while (dict.containsKey(COSName.PARENT)) {
        COSBase parent = dict.getDictionaryObject(COSName.PARENT);
        if (parent instanceof COSDictionary) {
            dict = (COSDictionary) parent;
            dict.setNeedToBeUpdated(true);
        }
    }

    document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true);
    //document.getDocumentCatalog().getStructureTreeRoot().getCOSObject().setNeedToBeUpdated(true);

    // Save the results and ensure that the document is properly closed:
    ByteArrayOutputStream confirmationPdfOut = new ByteArrayOutputStream();
    document.saveIncremental(confirmationPdfOut);
    document.close();

    return confirmationPdfOut.toByteArray();

}

我在 中发现所有 COSObject 都需要将标志 needToBeUpdated 设置为 true。

但是,当我尝试向文档添加另一页时,这仍然无济于事,因为当我尝试使用 Acrobat Reader.

验证签名时,第一个签名已失效

这可能吗? PDFBox 可以吗?

不,这不可能。不允许向已签名的 PDF 添加页面。

详细

I read in the adobe documentation under incremental updates that it may be possible.

确实可以在不触及以前修订的情况下向 PDF 添加更改。因此,如果之前的修订版已签名,签名数学上仍然有效,它仍然签署正确的散列值

但是 PDF 规范及其主要解释(即 Adob​​e 的解释)包含附加限制,请参见。 this stack overflow answer。正如您所发现的那样,最多允许对签名文档进行以下更改:

  • Adding signature fields
  • Adding or editing annotations
  • Supplying form field values
  • Digitally signing

除了检查数学有效性之外,至少 Adob​​e Acrobat (Reader) 会测试此类更改,即使许多其​​他验证服务不这样做也是如此。

因此,您向已签名的 PDF 添加附加页面并在不破坏第一个签名的情况下再次签名的任务无法实现。