【发布时间】:2023-03-23 19:27:02
【问题描述】:
我刚刚编写了将文件附加到 PDF 文档的代码。我已经在 PDFBox 页面中看到了代码。
PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setFile( "Test.txt" );
InputStream is = ...;
PDEmbeddedFile ef = new PDEmbeddedFile(doc, is );
ef.setSubtype( "test/plain" );
ef.setSize( data.length );
ef.setCreationDate( new GregorianCalendar() );
fs.setEmbeddedFile( ef );
Map efMap = new HashMap();
efMap.put( "My first attachment", fs );
efTree.setNames( efMap );
PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
names.setEmbeddedFiles( efTree );
doc.getDocumentCatalog().setNames( names );
doc.save("attachedPDF");
那,有效。
然后,我附加文件,并签署文件。 结果是 - 一切正常!
然后,我得到 已签名 文档(其中包含 附件),然后使用另一个附件 签署文档(我创建了修订版 2 . 换句话说,我将另一个文件附加到已签名的文档并再次签名)。结果是,没有旧文件。新文件覆盖了旧文件(签名也变得无效,因为更改了哈希 - 这是正确的);
所以,我已经完成了从 PDEmbeddedFilesNameTreeNode 获取 oldFiles 并添加到新文件映射的操作。
PDEmbeddedFilesNameTreeNode oldFiles=names.getEmbeddedFiles();
if(oldFiles!=null){
Map oldFilesMap = oldFiles.getNames();
Iterator iterator = oldFilesMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("The key is: " + mapEntry.getKey()+ ",value is :" + mapEntry.getValue());
efMap.put(mapEntry.getKey(), mapEntry.getValue());
}
}
efTree.setNames(efMap);
这行得通。但是当我创建第二个修订版时签名再次无效。 我认为,主要问题是,当我将新文件添加到同一个文件 NameDictionary 时,文档的哈希值会发生变化。
所以,我想,我应该在下一个版本中创建新的NameDictionary,可能是我错了(我不能使用已有的NameDictionary)。我不明白。我能知道什么?你觉得呢?
顺便说一句,我认为这对我来说是不正确的,下一次修订
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
【问题讨论】:
标签: pdf adobe digital-signature acrobat pdfbox