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

时间:2017-06-28 07:28:12

标签: java pdf digital-signature pdfbox

是否可以在已签名的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();

}

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

但是,在尝试向文档添加其他页面时仍然无效,因为当我尝试使用Acrobat Reader验证签名时,第一个签名失效。

甚至可能吗?是否可以使用PDFBox?

1 个答案:

答案 0 :(得分:3)

不,这是不可能的。不允许将页面添加到已签名的PDF中。

详细

  

我在增量更新下阅读了in the adobe documentation,表明可能会这样做。

实际上,可以在不触及前一版本的情况下添加对PDF的更改。因此,如果签署了以前的修订版,则签名在数学上仍然有效,仍然会签署正确的哈希值

PDF规范及其主要解释(由Adobe提供)包含其他限制,参见this stack overflow answer。如您所发现的那样,签名文件最多允许以下更改:

  
      
  • 添加签名字段
  •   
  • 添加或编辑注释
  •   
  • 提供表单字段值
  •   
  • 数字签名
  •   

除了检查数学有效性之外,至少Adobe Acrobat(Reader)会测试此类更改,即使许多其​​他验证服务没有。

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

相关问题