When using iText to sign PDF document with external signature, I have to prepare empty signature container first:
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setVisibleSignature(rectangle, 1, fieldName);
appearance.setCertificate(chain[0]);
ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
MakeSignature.signExternalContainer(appearance, external, 8192);
The last parameter of MakeSignature.signExternalContainer(appearance, external, 8192);
is estimatedSize
- the reserved size for the signature.
In case that the PDF has for example 10 000 bytes and estimatedSize
is set to 10 000 bytes and actual signature container has 1 000 bytes, then the resulting PDF will be of size of original PDF + estimatedSize
, instead of size of original PDF + size of signature container, which could be much larger.
Is there any way how to calculate the estimatedSize
parameter exactly to avoid such increase in size of signed PDF?
Or what are prerequisites to be able to calculate the exact size for the signature?
Are there other way how to sign PDF document using iText to avoid this?