Skip to main content

TimestampOptions Class

Contains Time Stamp Authority (TSA) server settings for signature timestamps.

Declaration

public class TimestampOptions extends JavaObject

Remarks

To add a timestamp to a signature, pass a TimestampOptions object to the Pkcs7DocumentSigner(options, timestampOptions) constructor. The API requests the timestamp from the TSA server when you save the document.

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

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);
            }
        }
    }
}

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
com.devexpress.system.JavaObject
TimestampOptions
See Also

TimestampOptions(String url)

Initializes a new instance of the TimestampOptions class with the specified TSA server URL.

Declaration

public TimestampOptions(String url)

Parameters

Name Type Description
url String

The URL of the Time Stamp Authority (TSA) server.

Exceptions

Type Description
ArgumentException

Thrown if the url parameter is null or an empty string.

Remarks

The API parses the URL when you save the document. If the URL is not a valid absolute URI, the PdfDocument.save method throws a UriFormatException.

Methods

getHashAlgorithm() Method

Returns the hash algorithm for timestamp requests.

Declaration

public HashAlgorithm getHashAlgorithm()

Returns

Type Description
HashAlgorithm

The hash algorithm.

getPassword() Method

Returns the password for TSA server authentication.

Declaration

public String getPassword()

Returns

Type Description
String

The password.

getUrl() Method

Returns the URL of the Time Stamp Authority (TSA) server.

Declaration

public String getUrl()

Returns

Type Description
String

The TSA server URL.

getUserName() Method

Returns the user name for TSA server authentication.

Declaration

public String getUserName()

Returns

Type Description
String

The user name.

setHashAlgorithm(HashAlgorithm value) Method

Sets the hash algorithm for timestamp requests.

Declaration

public void setHashAlgorithm(HashAlgorithm value)

Parameters

Name Type Description
value HashAlgorithm

The hash algorithm.

Remarks

The setHashAlgorithm method accepts the following values:

Value Description
SHA_256 Uses the SHA-256 hash algorithm.
SHA_384 Uses the SHA-384 hash algorithm.
SHA_512 Uses the SHA-512 hash algorithm.

If you do not call the setHashAlgorithm(value) method, the API uses HashAlgorithm.SHA_256.

The SignatureOptions.setHashAlgorithm(value) method specifies the hash algorithm of the signature.

setPassword(String value) Method

Sets the password for TSA server authentication.

Declaration

public void setPassword(String value)

Parameters

Name Type Description
value String

The password.

Remarks

The API sends the password to the TSA server only if you specify a user name (setUserName(String value)).

setUserName(String value) Method

Sets the user name for TSA server authentication.

Declaration

public void setUserName(String value)

Parameters

Name Type Description
value String

The user name.

Remarks

If you do not specify the user name or pass null or an empty string, the API sends timestamp requests without credentials and ignores the password specified by the setPassword(String value) method.