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

标签 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 可以吗?

最佳答案

不,这是不可能的。不允许向已签名的 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 添加附加页面并在不破坏第一个签名的情况下再次签名的任务无法实现。

关于java - 将附加页面添加到已签名的 pdf 中并再次签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796153/

相关文章:

java - 由于 Java 无法运行 SDK Manager

java - 将 BufferedImage 添加到 PDFBox 2.0 文档

java - 在 "if"语句内将 String 转换为 int

vb.net - 以编程方式打印到 pdf 打印机

java - 使用 PDBOX 签署大 PDF 文件

c# - 使用数字签名对强命名程序集进行签名是一种不好的做法吗?

java - 删除 Maven 中的传递依赖

.net - 注入(inject)staple命令时postscript编码不一致

javascript - 如何添加前端按钮来触发pdfcrowd的node.js API生成PDF

email - 如何检查加密的S/MIME消息是否也已签名,而不对其进行解密