title: Java iText sign PDF with external signature - container size estimation
body: <p>When using iText to sign PDF document with external signature, I have to prepare empty signature container first:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>The last parameter of <code>MakeSignature.signExternalContainer(appearance, external, 8192);</code> is <code>estimatedSize</code> - the reserved size for the signature.</p>
  
  <p>In case that the PDF has for example 10 000 bytes and <code>estimatedSize</code> 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 + <code>estimatedSize</code>, instead of size of original PDF + size of signature container, which could be much larger.</p>
  
  <p>Is there any way how to calculate the <code>estimatedSize</code> parameter exactly to avoid such increase in size of signed PDF?</p>
  
  <p>Or what are prerequisites to be able to calculate the exact size for the signature?</p>
  
  <p>Are there other way how to sign PDF document using iText to avoid this?</p>