I've just write code that attaches files to PDF Document. I've seen the code in PDFBox page.
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");
that, works.
Then, I've attached files, and sign document. result is that -everything works!
Then, I get the signed document (which have attachments), and then sign the document with another attachment (I create revision 2. In the other words, I attach another files to signed document and sign again). The result was that, there was no old file. New file have overwrite old files (signature become invalid too , because of changing hash- that's correct);
So, I've done so that I get oldFiles from PDEmbeddedFilesNameTreeNode and add to new file map.
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);
that works. but signature is again invalid when I create second revision. I think, The main problem is that, when I add new files to the same file NameDictionary, the hash of the document changes.
So, I think, I should create new NameDictionary in the next revision , may be I am wrong (I must not use existed NameDictionary). I dont understand. what can I do know? what do you think?
By the way, I think that is incorrect for me, for next revision
PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
that's my sample documents