PDF Signature Generation

Print E-mail  

The aim of this example is to use the TrustedX signing service to sign a PDF document. To understand this example, we recommend that you first understand the CMS/PKCS#7 Signature Generation example.

To try this example you must download the file, sampleAxis12.zip, and follow the instructions in Configuration of the Environment.

As in the case of the CMS/PKCS#7 Signature Generation example, you must use a SignRequest object to make a sign request; in this case the signature profile will be PDF

SignRequest sr = new SignRequest();
sr.setProfile(new URI(PROF_SIGN_PDF));

In this example a signature must be applied to a PDF document: demo-file.pdf. The data is encoded in Base64 and included in the request using the method setBase64Data() method of the Document object 

InputDocuments idoc = new InputDocuments();
Document doc = new Document();
Base64Data b64data = new Base64Data();
b64data.set_value(Util.readBinaryFile(path_in + signname));
b64data.setMimeType("application/pdf");
doc.setBase64Data(b64data);
idoc.setDocument(doc);
sr.setInputDocuments(idoc);

In the case of PDF signatures, we can indicate a number of attibutes that the signature must have. This example defines the following attributes: the signature validation method used by AdobeReader (validationMethod), the position of the signature (signaturePosition) and the type of signature used (signatureAlg). These attributes are included in the value of a string which is defined as a program constant; this string is assigned to an Axis Properties object 

To send the request we invoke the sign() operation; the response is collected in a SignResponse object. 

SignResponse srs = bindingDs.sign(sr);

After executing the example, we will receive a response (see below) and a document with the signature.

File saved successfully on: data/output/demo-file-signed.pdf