Styling signatures

jPDF Signer allows customization of the signature visual appearance in three different ways.

  1. Text - You may specify that the text be displayed within the signature area, including static and dynamic content. The dynamic content refers to attributes related to the certificate to be used during the signing process.
  2. Image - You may place an image on the signature area.
  3. Text and image - You may include both contents specified above.

In this tutorial, you will find three examples that reflect these different appearances.

Image Only

In this example we will add a sole image to the area of the signature form field.

Doing so, we will instruct the API to resize the image to fit the area automatically - all while keeping the same XY ratio of the original image.

// Open the keystore:
char[] pkcs12Password = "PASSWORD".toCharArray();
KeyStore ks = loadKeyStore(pkcs12Password);

// Recover the certificate from the keystore:
String keyAlias = "rsaentry3";
PrivateKey key = (PrivateKey) ks.getKey(keyAlias, pkcs12Password);

Certificate[] certs = ks.getCertificateChain(keyAlias); 

// Open the document:
PdfDocument doc = new PdfDocument("Doctobesigned.pdf"); 

// Initiate the writer preferences
Preferences prefs = new Preferences(); 
prefs.setSignatureType(Preferences.SIGN_WINDOWS);

doc.setWriterPreferences(prefs); 

// Recover the signature form field
SignatureField signatureField =
doc.getSignerModule().getSignature("Signature1"); 

// Create a new signature appearance object
SignatureAppearance app=new
SignatureAppearance();

// Load the mage to be shown in the signature form field:

AbstractImage img=ImageConvertor.getInstance().loadImage("sigimage.gif");

// Make sure the image fits the signature form field area and is resized without any deformation
app.setImage(img,SignatureAppearance.FIT_KEEP_RATIO);

// Hide the digital signature information (text):
app.setSignatureInfoContent(null,null);

// Register this new appearance with the signature form field:
signatureField.setSignatureAppearance(app);

// Sign the document:
signatureField.sign(key, certs); 

// Save the signed document as a new document:
doc.saveAs("DocumentSigned.pdf");
doc.close();

Text only

When specifying the text to be added in the signature area you have the option to add specific keywords within your text content.

During the signing process, the API will check for the presence of these keywords, replacing them by their corresponding values from the certificate used - as per the table below:

  • SignatureAppearance.INFOCONTACTINFO

The "Contact Information" stored in the certificate is used

  • SignatureAppearance.INFO_DATE

The date of the signing process

  • SignatureAppearance.INFO_LOCATION

The "Location" attribute stored in the certificate used

  • SignatureAppearance.INFO_REASON

The "Reason" attribute stored in the certificate used

  • SignatureAppearance.INFOSIGNERNAME

The "Signer Name" attribute stored in the certificate used

The default value of the text string is constructed as follows:

private String sigInfoContent = "Digitaly signed by " + INFO_SIGNER_NAME + "\n" + "Location: " +
INFO_LOCATION + "\n" + "Contact Info: " + INFO_CONTACT_INFO + "\n" + "Reason:
" + INFO_REASON + "\n" + "Date: " + INFO_DATE;

In the following example we will construct a text signature appearance containing only the signer name followed by the date when the document was signed.

// Same implementation as per the first example...
.... 

// Create a new signature appearance object:
SignatureAppearance app=new SignatureAppearance(); 

// Create a new string that will only contain the signer name and the date of signature
String str="Digitaly signed by " +
SignatureAppearance.INFO_SIGNER_NAME+"\n" + "Date: " +
SignatureAppearance.INFO_DATE;

// Register the string and let the API place it automatically on the signature
app.setSignatureInfoContent(str,null);

// Register this new appearance with the signature form field
signatureField.setSignatureAppearance(app); 

// Same implementation as per the first example...
....

Both appearances content

In the following example we will construct a signature appearance containing both images and text.

The text will be drawn on the second (right half) of the signature form field area. The image, meanwhile, will be resized automatically to fit the left side area.

// Same implementation as per the first example... :
.... 

// Create a new signature appearance object:
SignatureAppearance app=new SignatureAppearance(); 

// Load the image to be shown in the signature form field:
AbstractImage img=ImageConvertor.getInstance().loadImage("sigimage.gif");

// Make sure the image fits the signature form field area and is resized without any deformation
app.setImage(img,SignatureAppearance.FIT_KEEP_RATIO); 

// Create a new string that will only contain the signer name and the date of the signature
String str="Digitaly signed by " + SignatureAppearance.INFO_SIGNER_NAME+"\n" + "Date: " + SignatureAppearance.INFO_DATE; 

// Recover the bounding box of the form field and determine the coordinates
// corresponding to the right of this area where the information will be drawn:

Rectangle rectSignature=signatureField.getPosition(); 
int x=(int)Math.floor(rectSignature.getWidth()/2);
int y=0; 

int width=(int)Math.floor(rectSignature.getWidth()/2);
int height=(int)Math.floor(rectSignature.getHeight()); 

Rectangle rectInfo=new Rectangle(x,0,width, height); 

// Register the string while specifying its position:
app.setSignatureInfoContent(str,rectInfo);

// Register this new appearance with the signature form field:
signatureField.setSignatureAppearance(app); 

// Same implementation as per the first example... 
....