SignOptions
Inheritance: java.lang.Object
public class SignOptions
Allows to specify options for document signing.
To learn more, visit the Work with Digital Signatures documentation article.
Examples:
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());
}
// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
InputStream streamIn = new FileInputStream(getMyDir() + "Document.docx");
try {
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "DigitalSignatureUtil.SignDocument.docx");
try {
DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
} finally {
if (streamOut != null) streamOut.close();
}
} finally {
if (streamIn != null) streamIn.close();
}
Methods
Method | Description |
---|---|
getComments() | Specifies comments on the digital signature. |
getDecryptionPassword() | The password to decrypt source document. |
getProviderId() | Specifies the class ID of the signature provider. |
getSignTime() | The date of signing. |
getSignatureLineId() | Signature line identifier. |
getSignatureLineImage() | The image that will be shown in associated SignatureLine. |
getXmlDsigLevel() | Specifies the level of a digital signature based on XML-DSig standard. |
setComments(String value) | Specifies comments on the digital signature. |
setDecryptionPassword(String value) | The password to decrypt source document. |
setProviderId(UUID value) | Specifies the class ID of the signature provider. |
setSignTime(Date value) | The date of signing. |
setSignatureLineId(UUID value) | Signature line identifier. |
setSignatureLineImage(byte[] value) | The image that will be shown in associated SignatureLine. |
setXmlDsigLevel(int value) | Specifies the level of a digital signature based on XML-DSig standard. |
getComments()
public String getComments()
Specifies comments on the digital signature. Default value is empty string.
Examples:
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());
}
// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
InputStream streamIn = new FileInputStream(getMyDir() + "Document.docx");
try {
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "DigitalSignatureUtil.SignDocument.docx");
try {
DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
} finally {
if (streamOut != null) streamOut.close();
}
} finally {
if (streamIn != null) streamIn.close();
}
Returns: java.lang.String - The corresponding java.lang.String value.
getDecryptionPassword()
public String getDecryptionPassword()
The password to decrypt source document. Default value is empty string.
Remarks:
If OOXML document is encrypted, you should provide decryption password to decrypt source document before it will be signed. This is not required for documents in binary DOC format.
Examples:
Shows how to sign encrypted document file.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment, date, and decryption password which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");
}
// Set a local system filename for the unsigned input document, and an output filename for its new digitally signed copy.
String inputFileName = getMyDir() + "Encrypted.docx";
String outputFileName = getArtifactsDir() + "DigitalSignatureUtil.DecryptionPassword.docx";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);
Returns: java.lang.String - The corresponding java.lang.String value.
getProviderId()
public UUID getProviderId()
Specifies the class ID of the signature provider. Default value is Empty (all zeroes) Guid.
Remarks:
The cryptographic service provider (CSP) is an independent software module that actually performs cryptography algorithms for authentication, encoding, and encryption. MS Office reserves the value of {00000000-0000-0000-0000-000000000000} for its default signature provider.
The GUID of the additionally installed provider should be obtained from the documentation shipped with the provider.
In addition, all the installed cryptographic providers are enumerated in windows registry. It can be found in the following path: HKLM\SOFTWARE\Microsoft\Cryptography\Defaults\Provider. There is a key name “CP Service UUID” which corresponds to a GUID of signature provider.
Examples:
Shows how to sign a document with a personal certificate and a signature line.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner("vderyushev");
signatureLineOptions.setSignerTitle("QA");
signatureLineOptions.setEmail("vderyushev@aspose.com");
signatureLineOptions.setShowDate(true);
signatureLineOptions.setDefaultInstructions(false);
signatureLineOptions.setInstructions("Please sign here.");
signatureLineOptions.setAllowComments(true);
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setProviderId(UUID.fromString("CF5A7BB4-8F3C-4756-9DF6-BEF7F13259A2"));
Assert.assertFalse(signatureLine.isSigned());
Assert.assertFalse(signatureLine.isValid());
doc.save(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.docx");
Date currentDate = new Date();
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signatureLine.getId());
signOptions.setProviderId(signatureLine.getProviderId());
signOptions.setComments("Document was signed by vderyushev");
signOptions.setSignTime(currentDate);
CertificateHolder certHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
DigitalSignatureUtil.sign(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.docx",
getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.Signed.docx", certHolder, signOptions);
// Re-open our saved document, and verify that the "IsSigned" and "IsValid" properties both equal "true",
// indicating that the signature line contains a signature.
doc = new Document(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.Signed.docx");
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
signatureLine = shape.getSignatureLine();
Assert.assertTrue(signatureLine.isSigned());
Assert.assertTrue(signatureLine.isValid());
Returns: java.util.UUID - The corresponding java.util.UUID value.
getSignTime()
public Date getSignTime()
The date of signing. Default value is current time
Examples:
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());
}
// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
InputStream streamIn = new FileInputStream(getMyDir() + "Document.docx");
try {
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "DigitalSignatureUtil.SignDocument.docx");
try {
DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
} finally {
if (streamOut != null) streamOut.close();
}
} finally {
if (streamIn != null) streamIn.close();
}
Returns: java.util.Date - The corresponding java.util.Date value.
getSignatureLineId()
public UUID getSignatureLineId()
Signature line identifier. Default value is Empty (all zeroes) Guid.
Remarks:
When set, it associates SignatureLine with corresponding DigitalSignature.
Examples:
Shows how to add a signature line to a document, and then sign it using a digital certificate.
public static void sign() throws Exception {
String signPersonName = "Ron Williams";
String srcDocumentPath = getMyDir() + "Document.docx";
String dstDocumentPath = getArtifactsDir() + "SignDocumentCustom.Sign.docx";
String certificatePath = getMyDir() + "morzal.pfx";
String certificatePassword = "aw";
// We need to create simple list with test signers for this example.
createSignPersonData();
System.out.println("Test data successfully added!");
// Get sign person object by name of the person who must sign a document.
// This an example, in real use case you would return an object from a database.
SignPersonTestClass signPersonInfo = gSignPersonList.stream().filter(x -> x.getName() == signPersonName).findFirst().get();
if (signPersonInfo != null) {
signDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
System.out.println("Document successfully signed!");
} else {
System.out.println("Sign person does not exist, please check your parameters.");
}
// Now do something with a signed document, for example, save it to your database.
// Use 'new Document(dstDocumentPath)' for loading a signed document.
}
///
/// Signs the document obtained at the source location and saves it to the specified destination.
///
private static void signDocument(final String srcDocumentPath, final String dstDocumentPath,
final SignPersonTestClass signPersonInfo, final String certificatePath,
final String certificatePassword) throws Exception {
// Create new document instance based on a test file that we need to sign.
Document document = new Document(srcDocumentPath);
DocumentBuilder builder = new DocumentBuilder(document);
// Add info about responsible person who sign a document.
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner(signPersonInfo.getName());
signatureLineOptions.setSignerTitle(signPersonInfo.getPosition());
// Add signature line for responsible person who sign a document.
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setId(signPersonInfo.getPersonId());
// Save a document with line signatures into temporary file for future signing.
builder.getDocument().save(dstDocumentPath);
// Create holder of certificate instance based on your personal certificate.
// This is the test certificate generated for this example.
CertificateHolder certificateHolder = CertificateHolder.create(certificatePath, certificatePassword);
// Link our signature line with personal signature.
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signPersonInfo.getPersonId());
signOptions.setSignatureLineImage(signPersonInfo.getImage());
// Sign a document which contains signature line with personal certificate.
DigitalSignatureUtil.sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}
///
/// Create test data that contains info about sing persons.
///
private static void createSignPersonData() throws IOException {
InputStream inputStream = new FileInputStream(getImageDir() + "Logo.jpg");
gSignPersonList = new ArrayList<>();
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Ron Williams", "Chief Executive Officer",
DocumentHelper.getBytesFromStream(inputStream)));
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Stephen Morse", "Head of Compliance",
DocumentHelper.getBytesFromStream(inputStream)));
}
private static ArrayList gSignPersonList;
Returns: java.util.UUID - The corresponding java.util.UUID value.
getSignatureLineImage()
public byte[] getSignatureLineImage()
The image that will be shown in associated SignatureLine. Default value is null .
Examples:
Shows how to add a signature line to a document, and then sign it using a digital certificate.
public static void sign() throws Exception {
String signPersonName = "Ron Williams";
String srcDocumentPath = getMyDir() + "Document.docx";
String dstDocumentPath = getArtifactsDir() + "SignDocumentCustom.Sign.docx";
String certificatePath = getMyDir() + "morzal.pfx";
String certificatePassword = "aw";
// We need to create simple list with test signers for this example.
createSignPersonData();
System.out.println("Test data successfully added!");
// Get sign person object by name of the person who must sign a document.
// This an example, in real use case you would return an object from a database.
SignPersonTestClass signPersonInfo = gSignPersonList.stream().filter(x -> x.getName() == signPersonName).findFirst().get();
if (signPersonInfo != null) {
signDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
System.out.println("Document successfully signed!");
} else {
System.out.println("Sign person does not exist, please check your parameters.");
}
// Now do something with a signed document, for example, save it to your database.
// Use 'new Document(dstDocumentPath)' for loading a signed document.
}
///
/// Signs the document obtained at the source location and saves it to the specified destination.
///
private static void signDocument(final String srcDocumentPath, final String dstDocumentPath,
final SignPersonTestClass signPersonInfo, final String certificatePath,
final String certificatePassword) throws Exception {
// Create new document instance based on a test file that we need to sign.
Document document = new Document(srcDocumentPath);
DocumentBuilder builder = new DocumentBuilder(document);
// Add info about responsible person who sign a document.
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner(signPersonInfo.getName());
signatureLineOptions.setSignerTitle(signPersonInfo.getPosition());
// Add signature line for responsible person who sign a document.
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setId(signPersonInfo.getPersonId());
// Save a document with line signatures into temporary file for future signing.
builder.getDocument().save(dstDocumentPath);
// Create holder of certificate instance based on your personal certificate.
// This is the test certificate generated for this example.
CertificateHolder certificateHolder = CertificateHolder.create(certificatePath, certificatePassword);
// Link our signature line with personal signature.
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signPersonInfo.getPersonId());
signOptions.setSignatureLineImage(signPersonInfo.getImage());
// Sign a document which contains signature line with personal certificate.
DigitalSignatureUtil.sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}
///
/// Create test data that contains info about sing persons.
///
private static void createSignPersonData() throws IOException {
InputStream inputStream = new FileInputStream(getImageDir() + "Logo.jpg");
gSignPersonList = new ArrayList<>();
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Ron Williams", "Chief Executive Officer",
DocumentHelper.getBytesFromStream(inputStream)));
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Stephen Morse", "Head of Compliance",
DocumentHelper.getBytesFromStream(inputStream)));
}
private static ArrayList gSignPersonList;
Returns: byte[] - The corresponding byte[] value.
getXmlDsigLevel()
public int getXmlDsigLevel()
Specifies the level of a digital signature based on XML-DSig standard. The default value is XmlDsigLevel.XML_D_SIG.
Remarks:
Different levels of XAdES signatures can be created starting from Office 2010.
Examples:
Shows how to sign document based on XML-DSig standard.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
SignOptions signOptions = new SignOptions(); { signOptions.setXmlDsigLevel(XmlDsigLevel.X_AD_ES_EPES); }
String inputFileName = getMyDir() + "Document.docx";
String outputFileName = getArtifactsDir() + "DigitalSignatureUtil.XmlDsig.docx";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);
Returns: int - The corresponding int value. The returned value is one of XmlDsigLevel constants.
setComments(String value)
public void setComments(String value)
Specifies comments on the digital signature. Default value is empty string.
Examples:
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());
}
// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
InputStream streamIn = new FileInputStream(getMyDir() + "Document.docx");
try {
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "DigitalSignatureUtil.SignDocument.docx");
try {
DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
} finally {
if (streamOut != null) streamOut.close();
}
} finally {
if (streamIn != null) streamIn.close();
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setDecryptionPassword(String value)
public void setDecryptionPassword(String value)
The password to decrypt source document. Default value is empty string.
Remarks:
If OOXML document is encrypted, you should provide decryption password to decrypt source document before it will be signed. This is not required for documents in binary DOC format.
Examples:
Shows how to sign encrypted document file.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment, date, and decryption password which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("Comment");
signOptions.setSignTime(new Date());
signOptions.setDecryptionPassword("docPassword");
}
// Set a local system filename for the unsigned input document, and an output filename for its new digitally signed copy.
String inputFileName = getMyDir() + "Encrypted.docx";
String outputFileName = getArtifactsDir() + "DigitalSignatureUtil.DecryptionPassword.docx";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The corresponding java.lang.String value. |
setProviderId(UUID value)
public void setProviderId(UUID value)
Specifies the class ID of the signature provider. Default value is Empty (all zeroes) Guid.
Remarks:
The cryptographic service provider (CSP) is an independent software module that actually performs cryptography algorithms for authentication, encoding, and encryption. MS Office reserves the value of {00000000-0000-0000-0000-000000000000} for its default signature provider.
The GUID of the additionally installed provider should be obtained from the documentation shipped with the provider.
In addition, all the installed cryptographic providers are enumerated in windows registry. It can be found in the following path: HKLM\SOFTWARE\Microsoft\Cryptography\Defaults\Provider. There is a key name “CP Service UUID” which corresponds to a GUID of signature provider.
Examples:
Shows how to sign a document with a personal certificate and a signature line.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner("vderyushev");
signatureLineOptions.setSignerTitle("QA");
signatureLineOptions.setEmail("vderyushev@aspose.com");
signatureLineOptions.setShowDate(true);
signatureLineOptions.setDefaultInstructions(false);
signatureLineOptions.setInstructions("Please sign here.");
signatureLineOptions.setAllowComments(true);
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setProviderId(UUID.fromString("CF5A7BB4-8F3C-4756-9DF6-BEF7F13259A2"));
Assert.assertFalse(signatureLine.isSigned());
Assert.assertFalse(signatureLine.isValid());
doc.save(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.docx");
Date currentDate = new Date();
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signatureLine.getId());
signOptions.setProviderId(signatureLine.getProviderId());
signOptions.setComments("Document was signed by vderyushev");
signOptions.setSignTime(currentDate);
CertificateHolder certHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
DigitalSignatureUtil.sign(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.docx",
getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.Signed.docx", certHolder, signOptions);
// Re-open our saved document, and verify that the "IsSigned" and "IsValid" properties both equal "true",
// indicating that the signature line contains a signature.
doc = new Document(getArtifactsDir() + "DocumentBuilder.SignatureLineProviderId.Signed.docx");
Shape shape = (Shape) doc.getChild(NodeType.SHAPE, 0, true);
signatureLine = shape.getSignatureLine();
Assert.assertTrue(signatureLine.isSigned());
Assert.assertTrue(signatureLine.isValid());
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.UUID | The corresponding java.util.UUID value. |
setSignTime(Date value)
public void setSignTime(Date value)
The date of signing. Default value is current time
Examples:
Shows how to digitally sign documents.
// Create an X.509 certificate from a PKCS#12 store, which should contain a private key.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
// Create a comment and date which will be applied with our new digital signature.
SignOptions signOptions = new SignOptions();
{
signOptions.setComments("My comment");
signOptions.setSignTime(new Date());
}
// Take an unsigned document from the local file system via a file stream,
// then create a signed copy of it determined by the filename of the output file stream.
InputStream streamIn = new FileInputStream(getMyDir() + "Document.docx");
try {
OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "DigitalSignatureUtil.SignDocument.docx");
try {
DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
} finally {
if (streamOut != null) streamOut.close();
}
} finally {
if (streamIn != null) streamIn.close();
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.Date | The corresponding java.util.Date value. |
setSignatureLineId(UUID value)
public void setSignatureLineId(UUID value)
Signature line identifier. Default value is Empty (all zeroes) Guid.
Remarks:
When set, it associates SignatureLine with corresponding DigitalSignature.
Examples:
Shows how to add a signature line to a document, and then sign it using a digital certificate.
public static void sign() throws Exception {
String signPersonName = "Ron Williams";
String srcDocumentPath = getMyDir() + "Document.docx";
String dstDocumentPath = getArtifactsDir() + "SignDocumentCustom.Sign.docx";
String certificatePath = getMyDir() + "morzal.pfx";
String certificatePassword = "aw";
// We need to create simple list with test signers for this example.
createSignPersonData();
System.out.println("Test data successfully added!");
// Get sign person object by name of the person who must sign a document.
// This an example, in real use case you would return an object from a database.
SignPersonTestClass signPersonInfo = gSignPersonList.stream().filter(x -> x.getName() == signPersonName).findFirst().get();
if (signPersonInfo != null) {
signDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
System.out.println("Document successfully signed!");
} else {
System.out.println("Sign person does not exist, please check your parameters.");
}
// Now do something with a signed document, for example, save it to your database.
// Use 'new Document(dstDocumentPath)' for loading a signed document.
}
///
/// Signs the document obtained at the source location and saves it to the specified destination.
///
private static void signDocument(final String srcDocumentPath, final String dstDocumentPath,
final SignPersonTestClass signPersonInfo, final String certificatePath,
final String certificatePassword) throws Exception {
// Create new document instance based on a test file that we need to sign.
Document document = new Document(srcDocumentPath);
DocumentBuilder builder = new DocumentBuilder(document);
// Add info about responsible person who sign a document.
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner(signPersonInfo.getName());
signatureLineOptions.setSignerTitle(signPersonInfo.getPosition());
// Add signature line for responsible person who sign a document.
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setId(signPersonInfo.getPersonId());
// Save a document with line signatures into temporary file for future signing.
builder.getDocument().save(dstDocumentPath);
// Create holder of certificate instance based on your personal certificate.
// This is the test certificate generated for this example.
CertificateHolder certificateHolder = CertificateHolder.create(certificatePath, certificatePassword);
// Link our signature line with personal signature.
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signPersonInfo.getPersonId());
signOptions.setSignatureLineImage(signPersonInfo.getImage());
// Sign a document which contains signature line with personal certificate.
DigitalSignatureUtil.sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}
///
/// Create test data that contains info about sing persons.
///
private static void createSignPersonData() throws IOException {
InputStream inputStream = new FileInputStream(getImageDir() + "Logo.jpg");
gSignPersonList = new ArrayList<>();
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Ron Williams", "Chief Executive Officer",
DocumentHelper.getBytesFromStream(inputStream)));
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Stephen Morse", "Head of Compliance",
DocumentHelper.getBytesFromStream(inputStream)));
}
private static ArrayList gSignPersonList;
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.util.UUID | The corresponding java.util.UUID value. |
setSignatureLineImage(byte[] value)
public void setSignatureLineImage(byte[] value)
The image that will be shown in associated SignatureLine. Default value is null .
Examples:
Shows how to add a signature line to a document, and then sign it using a digital certificate.
public static void sign() throws Exception {
String signPersonName = "Ron Williams";
String srcDocumentPath = getMyDir() + "Document.docx";
String dstDocumentPath = getArtifactsDir() + "SignDocumentCustom.Sign.docx";
String certificatePath = getMyDir() + "morzal.pfx";
String certificatePassword = "aw";
// We need to create simple list with test signers for this example.
createSignPersonData();
System.out.println("Test data successfully added!");
// Get sign person object by name of the person who must sign a document.
// This an example, in real use case you would return an object from a database.
SignPersonTestClass signPersonInfo = gSignPersonList.stream().filter(x -> x.getName() == signPersonName).findFirst().get();
if (signPersonInfo != null) {
signDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword);
System.out.println("Document successfully signed!");
} else {
System.out.println("Sign person does not exist, please check your parameters.");
}
// Now do something with a signed document, for example, save it to your database.
// Use 'new Document(dstDocumentPath)' for loading a signed document.
}
///
/// Signs the document obtained at the source location and saves it to the specified destination.
///
private static void signDocument(final String srcDocumentPath, final String dstDocumentPath,
final SignPersonTestClass signPersonInfo, final String certificatePath,
final String certificatePassword) throws Exception {
// Create new document instance based on a test file that we need to sign.
Document document = new Document(srcDocumentPath);
DocumentBuilder builder = new DocumentBuilder(document);
// Add info about responsible person who sign a document.
SignatureLineOptions signatureLineOptions = new SignatureLineOptions();
signatureLineOptions.setSigner(signPersonInfo.getName());
signatureLineOptions.setSignerTitle(signPersonInfo.getPosition());
// Add signature line for responsible person who sign a document.
SignatureLine signatureLine = builder.insertSignatureLine(signatureLineOptions).getSignatureLine();
signatureLine.setId(signPersonInfo.getPersonId());
// Save a document with line signatures into temporary file for future signing.
builder.getDocument().save(dstDocumentPath);
// Create holder of certificate instance based on your personal certificate.
// This is the test certificate generated for this example.
CertificateHolder certificateHolder = CertificateHolder.create(certificatePath, certificatePassword);
// Link our signature line with personal signature.
SignOptions signOptions = new SignOptions();
signOptions.setSignatureLineId(signPersonInfo.getPersonId());
signOptions.setSignatureLineImage(signPersonInfo.getImage());
// Sign a document which contains signature line with personal certificate.
DigitalSignatureUtil.sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions);
}
///
/// Create test data that contains info about sing persons.
///
private static void createSignPersonData() throws IOException {
InputStream inputStream = new FileInputStream(getImageDir() + "Logo.jpg");
gSignPersonList = new ArrayList<>();
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Ron Williams", "Chief Executive Officer",
DocumentHelper.getBytesFromStream(inputStream)));
gSignPersonList.add(new SignPersonTestClass(UUID.randomUUID(), "Stephen Morse", "Head of Compliance",
DocumentHelper.getBytesFromStream(inputStream)));
}
private static ArrayList gSignPersonList;
Parameters:
Parameter | Type | Description |
---|---|---|
value | byte[] | The corresponding byte[] value. |
setXmlDsigLevel(int value)
public void setXmlDsigLevel(int value)
Specifies the level of a digital signature based on XML-DSig standard. The default value is XmlDsigLevel.XML_D_SIG.
Remarks:
Different levels of XAdES signatures can be created starting from Office 2010.
Examples:
Shows how to sign document based on XML-DSig standard.
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw");
SignOptions signOptions = new SignOptions(); { signOptions.setXmlDsigLevel(XmlDsigLevel.X_AD_ES_EPES); }
String inputFileName = getMyDir() + "Document.docx";
String outputFileName = getArtifactsDir() + "DigitalSignatureUtil.XmlDsig.docx";
DigitalSignatureUtil.sign(inputFileName, outputFileName, certificateHolder, signOptions);
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The corresponding int value. The value must be one of XmlDsigLevel constants. |