Skip to content
Advertisement

Why is the Digital Signature not visible in some PDF documents using iText 7.1.11?

My project digitally signs PDF documents using a digital certificate and displays the signature at the bottom left of the document. It had always worked well until now, there are some documents that are digitally signed but it is not shown although it is recognized that the rectangle where it is visible is. Could someone help with this, I leave a snippet that handles the digital signature. Here I leave a screenshot of how the digital signature is in a PDF document, I show it in the lower left corner of the document.

EXAMPLES

In this link I have shared example pdf documents of the problem and without it, I will detail them below:

  • ok_unsigned.pdf file: it is a document that when passing through my project is signed well, becoming the ok_signed.pdf file (this is the norm so far)
  • ok_signed.pdf file: it is the digitally signed ok_unsigned.pdf file, it is a case of success
  • bad_unsigned.pdf file: it is a blank document that when digitally signed (bad_signed.pdf) the issue in question appears
  • bad_signed.pdf file: it is a document with the issue in question, digitally signed but without visually seeing the signature field.

CODE

        try {
            BouncyCastleProvider providerBC = new BouncyCastleProvider();
            Security.addProvider(providerBC);

            KeyStore ks = KeyStore.getInstance("pkcs12");
            ks.load(new FileInputStream(keystore), password);
            String alias = ks.aliases().nextElement();
            Certificate[] chain = ks.getCertificateChain(alias);
            PrivateKey pk = (PrivateKey) ks.getKey(alias, password);

            PdfReader reader = new PdfReader(src);
            FileOutputStream fos = new FileOutputStream(new File(dest));
            PdfSigner signer = new PdfSigner(reader, fos, new StampingProperties());
            Rectangle rect = new Rectangle(10, 10, 150, 50);
            PdfSignatureAppearance appearance = signer.getSignatureAppearance();
            
            appearance.setPageRect(rect)
                    .setCertificate(chain[0])
                    .setReasonCaption("")
                    .setLocationCaption("")
                    .setSignatureCreator("SignerJAGC - iText 7.1.11")
                    .setPageNumber(1);
            signer.setFieldName("Banca en Línea - Envío de Documentos");
            signer.setSignDate(new GregorianCalendar());
            signer.setCertificationLevel(PdfSigner.CERTIFIED_NO_CHANGES_ALLOWED);

            IExternalDigest digest = new BouncyCastleDigest();
            IExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, providerBC.getName());

            signer.signDetached(digest, signature, chain, null, null, null, 0, SUBFILTER);
            System.out.println("SIGNED");
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }```

Advertisement

Answer

As @mkl said your coordinates might be outside of the page visible area as not all PDF pages have the bottom left corner at (0, 0).

Try creating the signature rectangle like this:

Rectangle rect = new Rectangle(
    yourPageCropBoxLowerLeftX + 10, 
    yourPageCropBoxLowerLeftY + 10, 
    yourPageCropBoxLowerLeftX + 10 + yourSignatureWidth, 
    yourPageCropBoxLowerLeftY + 10 + yourSignatureHeight);

You just have to see how you can read the page’s crop box coordinates, the lower left corner, as I’m not familiar with iText API.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement