Search code examples
javaitextcertificateitext7

iText Sign PDF with custom "Signed by" name


I can sign a PDF using iText 7.

Now, I want to show a different name than the one which is in Certificate, like a surname (not only on the PDF itself with the appearance but also in the "Rev1: Signed by X" in Adobe).

This is the code that I use actually to sign PDF :

PdfReader reader = new PdfReader(src);
PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), new StampingProperties().useAppendMode());

// Create the signature appearance
PdfPage page = signer.getDocument().getPage(1);
Rectangle rect = new Rectangle(10, 10, 120, 80);
PdfSignatureAppearance appearance = signer.getSignatureAppearance().setReason(reason).setPageNumber(1)
.setLocation(location).setPageRect(rect);

signer.setFieldName("sig");

IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
IExternalDigest digest = new BouncyCastleDigest();

// Sign the document using the detached mode, CMS or CAdES equivalent.
signer.signDetached(digest, pks, chain, null, null, null, 0, signatureType);

Solution

  • You ask how to show a different name than the one which is in Certificate, like a surname (not only on the PDF itself with the appearance but also in the "Rev1: Signed by X" in Adobe).

    This essentially are two questions at once, one on how to customize the text in the signature appearance on the document page and one on how to customize the text shown by Adobe Acrobat in its Signature Panel.

    Customize the Text in the Signature Appearance on the Document Page

    Here you have essentially two relevant helpers:

    • The PdfSignatureAppearance method setLayer2Text allow you to set the description text in the signature visualization. You can change its style using setLayer2Font, setLayer2FontSize, and setLayer2FontColor.
    • The PdfSignatureAppearance method getLayer2 returns a PdfFormXObject on which you can create a completely custom signature visualization. You can also use getLayer0 to return a PdfFormXObject which serves as a background.

    You can choose your helper depending on the degree of customization required.

    Customize the Text Shown by Adobe Acrobat in its Signature Panel

    This is something else entirely. As this panel is outside of the document area, the PDF specification is not dictating the contents of that panel, it is up to Adobe what information goes there. Thus, what you hope to achieve can only to a certain degree be managed if at all.

    Having experimented a bit using a fairly current Adobe Acrobat Reader it turns out that under certain circumstances the value of the Name entry of the signature dictionary (if set at all) is used here instead of certificate information.

    You can set that entry using signature events like this:

    PdfSigner pdfSigner = new PdfSigner(...);
    pdfSigner.setSignatureEvent(new PdfSigner.ISignatureEvent() {
        @Override
        public void getSignatureDictionary(PdfSignature sig) {
            sig.setName("A Custom Signer");
        }
    });
    ...
    

    Having signed with this addition Adobe Acrobat sometimes uses "A Custom Signer" instead of the certificate information in its signature panel.

    • If you switch off automatic verification during opening in Acrobat Reader

      Preferences

      and then open the PDF, you see

      Requires Validation

      After explicitly requesting validation, you get

      Valid

      As you see, "A Custom Signer" all the time.

    • If on the other hand you activate automatic verification upon opening in the preferences, you get

      Automatically Valid

      As you see, here information from the certificate is used.

    • Furthermore, you get "A Custom Signer" always if Adobe Acrobat cannot properly read the signature (e.g. due to an unknown signing algorithm).