There are two errors in your reasoning. One that can be fixed, one that is a not a technical error, but a reasoning error that can't be fixed.
The technical error:
You close the reader
before you close the document
. That's too early. PdfCopy
and Document
need access to the reader when document.close()
is invoked. You've already closed the reader
instance in the loop, hance the java.lang.IllegalStateException
informing you that the resource is Already closed.
The problem with your assumption: Digital signatures are a way to guarantee the integrity of a document. When somebody signs a document with 5 pages, that person wants to make sure he signed those 5 pages, not more, not less. You are now going to add pages. That will always break the signature!
If you want to combine PDF documents that are digitally signed:
- it is forbidden to assemble those documents: if you do, the signatures will either disappear or break.
- your only option is to combine the documents in a portable collection. In this case, a master PDF acts as a package or portfolio to store the separate PDFs. The PDFs as such are kept intact (if not: the signature shall break).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…