Skip to main content

Pkcs7DocumentSigner Class

A document signer that creates a PKCS#7 signature with an optional timestamp and a specified signature profile (PDF or PAdES).

Declaration

public class Pkcs7DocumentSigner extends DocumentSigner

Remarks

Use the Pkcs7DocumentSigner class to add a timestamp to a signature or to create a PAdES signature. Pass the signer to the PdfDocument.addSignature(field, signer) method. The API signs the field when you save the document.

The signer gets the certificate, hash algorithm, and signer information from the SignatureOptions object passed to the Pkcs7DocumentSigner constructor (Pkcs7DocumentSigner.getOptions() method).

To add a timestamp to the signature, call one of the following constructors:

  • Pkcs7DocumentSigner(options, timestampOptions) - creates a signer that requests a timestamp from the TSA server specified by a TimestampOptions object.
  • Pkcs7DocumentSigner(options, tsaClient) - creates a signer that requests a timestamp from an ITsaClient implementation (for example, a TsaClient object).

The Pkcs7DocumentSigner(options) constructor creates a signer without a timestamp.

Call the setProfile(SignatureProfile value) method to specify the signature profile.

Refer to the following help topic for additional information: Sign PDF Documents — PDF Document API for Java.

Example

Create a PAdES Signature

The following code snippet creates a PAdES signature:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream);
             InputStream certificateStream = Files.newInputStream(Path.of("certificate.pfx"))) {
            // Create a signature field and bind it to a widget on the first page.
            SignatureField signatureField = new SignatureField("Signature1");
            pdfDocument.getFields().add(signatureField);
            pdfDocument.getPages().getFirst().getAnnotations().add(
                    new SignatureWidgetAnnotation(signatureField, new RectangleF(20, 400, 200, 60)));

            // Specify the certificate and signer information.
            SignatureOptions options = new SignatureOptions();
            options.setCertificate(certificateStream, "CERTIFICATE_PASSWORD");
            options.setReason("Approved");

            // Create a PAdES signature.
            Pkcs7DocumentSigner signer = new Pkcs7DocumentSigner(options);
            signer.setProfile(SignatureProfile.P_AD_ES_BES);

            pdfDocument.addSignature(signatureField, signer);

            Files.createDirectories(Path.of("output"));
            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
JavaObject
DocumentSigner
Pkcs7DocumentSigner
See Also

Constructors

Pkcs7DocumentSigner(SignatureOptions options, ITsaClient tsaClient) Constructor

Initializes a new instance of the Pkcs7DocumentSigner class with the specified signature options and timestamp client.

Declaration

public Pkcs7DocumentSigner(SignatureOptions options, ITsaClient tsaClient)

Parameters

Name Type Description
options SignatureOptions

An object that contains the signature certificate, hash algorithm, and signer information.

tsaClient com.devexpress.office.tsp.ITsaClient

A client that requests timestamps from a Time Stamp Authority (TSA). Pass null to create a signature without a timestamp.

Exceptions

Type Description
ArgumentNullException

Thrown if the options parameter is null.

Remarks

Pass a TsaClient object or a custom ITsaClient implementation to the Pkcs7DocumentSigner(options, tsaClient) constructor. The API requests the timestamp when you save the document.

Pkcs7DocumentSigner(SignatureOptions options, TimestampOptions timestampOptions) Constructor

Initializes a new instance of the Pkcs7DocumentSigner class with the specified signature and timestamp options.

Declaration

public Pkcs7DocumentSigner(SignatureOptions options, TimestampOptions timestampOptions)

Parameters

Name Type Description
options SignatureOptions

An object that contains the signature certificate, hash algorithm, and signer information.

timestampOptions TimestampOptions

An object that contains Time Stamp Authority (TSA) server settings. Pass null to create a signature without a timestamp.

Remarks

The signer requests a timestamp from the TSA server specified by the TimestampOptions.getUrl() method. The API sends the request when you save the document.

If the TSA server requires authentication, call TimestampOptions.setUserName(value) and TimestampOptions.setPassword(value) methods to specify credentials.

Example: Create a PAdES Signature with a Timestamp

The following code snippet creates a PAdES signature with a timestamp:

import java.io.*;
import java.nio.file.*;
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (InputStream inputStream = Files.newInputStream(Path.of("Document.pdf"));
             PdfDocument pdfDocument = new PdfDocument(inputStream);
             InputStream certificateStream = Files.newInputStream(Path.of("certificate.pfx"))) {
            // Create a signature field and bind it to a widget on the first page.
            SignatureField signatureField = new SignatureField("Signature1");
            pdfDocument.getFields().add(signatureField);
            pdfDocument.getPages().getFirst().getAnnotations().add(
                    new SignatureWidgetAnnotation(signatureField, new RectangleF(20, 400, 200, 60)));

            // Specify the certificate and signer information.
            SignatureOptions options = new SignatureOptions();
            options.setCertificate(certificateStream, "CERTIFICATE_PASSWORD");
            options.setHashAlgorithm(HashAlgorithm.SHA_256);
            options.setReason("Approved");

            // Request a timestamp from a TSA server.
            TimestampOptions timestampOptions = new TimestampOptions("https://freetsa.org/tsr");
            timestampOptions.setHashAlgorithm(HashAlgorithm.SHA_256);

            // Create a PAdES signature.
            Pkcs7DocumentSigner signer = new Pkcs7DocumentSigner(options, timestampOptions);
            signer.setProfile(SignatureProfile.P_AD_ES_BES);

            pdfDocument.addSignature(signatureField, signer);

            Files.createDirectories(Path.of("output"));
            try (OutputStream outputStream = Files.newOutputStream(Path.of("output", "Document.pdf"))) {
                pdfDocument.save(outputStream);
            }
        }
    }
}

Pkcs7DocumentSigner(SignatureOptions options) Constructor

Initializes a new instance of the Pkcs7DocumentSigner class with the specified signature options.

Declaration

public Pkcs7DocumentSigner(SignatureOptions options)

Parameters

Name Type Description
options SignatureOptions

An object that contains the signature certificate, hash algorithm, and signer information.

Remarks

The signer created with the Pkcs7DocumentSigner(options) constructor does not add a timestamp to the signature.

To create a PAdES signature without a timestamp, call the setProfile(SignatureProfile.P_AD_ES_BES) method of the created signer.

Methods

getProfile() Method

Returns the signature profile.

Declaration

public SignatureProfile getProfile()

Returns

Type Description
SignatureProfile

The signature profile.

setProfile(SignatureProfile value) Method

Sets the signature profile.

Declaration

public void setProfile(SignatureProfile value)

Parameters

Name Type Description
value SignatureProfile

The signature profile.

Remarks

The setProfile method accepts the following values:

Value Description
PDF A standard PDF signature in PKCS#7 format.
P_AD_ES_BES A PAdES (PDF Advanced Electronic Signatures) signature with the Basic Electronic Signature (BES) profile.

If you do not call the setProfile(value) method, the API uses SignatureProfile.PDF. To create a PAdES signature, call the setProfile(SignatureProfile.P_AD_ES_BES) method.

The API applies the signature profile when you save the document.