java.lang.Objectcom.aspose.words.DigitalSignatureUtil
public abstract class DigitalSignatureUtil
Since digital signature works with file content rather than Document Object Model these methods are put into a separate class. Supported formats are Example:
// Remove all signatures from the document using string parameters
Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
String outFileName = getArtifactsDir() + "Document.NoSignatures.FromString.docx";
DigitalSignatureUtil.removeAllSignatures(doc.getOriginalFileName(), outFileName);
// Remove all signatures from the document using stream parameters
FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.NoSignatures.FromInputStream.doc");
DigitalSignatureUtil.removeAllSignatures(streamIn, streamOut);
Method Summary | ||
---|---|---|
static DigitalSignatureCollection | loadSignatures(java.io.InputStream stream) | |
Loads digital signatures from document using stream. | ||
static DigitalSignatureCollection | loadSignatures(java.lang.String fileName) | |
Loads digital signatures from document. | ||
static void | removeAllSignatures(java.io.InputStream srcStream, java.io.OutputStream dstStream) | |
Removes all digital signatures from document in source stream and writes unsigned document to destination stream.
Output will be written to the start of stream and stream size will be updated with content length. |
||
static void | removeAllSignatures(java.lang.String srcFileName, java.lang.String dstFileName) | |
Removes all digital signatures from source file and writes unsigned file to destination file. | ||
static void | sign(java.io.InputStream srcStream, java.io.OutputStream dstStream, CertificateHolder certHolder) | |
Signs source document using given Document should be either Output will be written to the start of stream and stream size will be updated with content length. |
||
static void | sign(java.io.InputStream srcStream, java.io.OutputStream dstStream, CertificateHolder certHolder, SignOptions signOptions) | |
Signs source document using given Document should be either Output will be written to the start of stream and stream size will be updated with content length. |
||
static void | sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder) | |
Signs source document using given Document should be either |
||
static void | sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder, SignOptions signOptions) | |
Signs source document using given Document should be either |
Method Detail |
---|
loadSignatures | |
public static DigitalSignatureCollection loadSignatures(java.io.InputStream stream) throws java.lang.Exception |
stream
- Stream with the document.Example:
Shows how to load all existing signatures from a document.// Load all signatures from the document using string parameters DigitalSignatureCollection digitalSignatures = DigitalSignatureUtil.loadSignatures(getMyDir() + "Document.DigitalSignature.docx"); // Load all signatures from the document using stream parameters InputStream stream = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx"); digitalSignatures = DigitalSignatureUtil.loadSignatures(stream);
loadSignatures | |
public static DigitalSignatureCollection loadSignatures(java.lang.String fileName) throws java.lang.Exception |
fileName
- Path to the document.Example:
Shows how to load all existing signatures from a document.// Load all signatures from the document using string parameters DigitalSignatureCollection digitalSignatures = DigitalSignatureUtil.loadSignatures(getMyDir() + "Document.DigitalSignature.docx"); // Load all signatures from the document using stream parameters InputStream stream = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx"); digitalSignatures = DigitalSignatureUtil.loadSignatures(stream);
removeAllSignatures | |
public static void removeAllSignatures(java.io.InputStream srcStream, java.io.OutputStream dstStream) throws java.lang.Exception |
Output will be written to the start of stream and stream size will be updated with content length.
Example:
Shows how to remove every signature from a document.// Remove all signatures from the document using string parameters Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx"); String outFileName = getArtifactsDir() + "Document.NoSignatures.FromString.docx"; DigitalSignatureUtil.removeAllSignatures(doc.getOriginalFileName(), outFileName); // Remove all signatures from the document using stream parameters FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx"); FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.NoSignatures.FromInputStream.doc"); DigitalSignatureUtil.removeAllSignatures(streamIn, streamOut);
removeAllSignatures | |
public static void removeAllSignatures(java.lang.String srcFileName, java.lang.String dstFileName) throws java.lang.Exception |
Example:
Shows how to remove every signature from a document.// Remove all signatures from the document using string parameters Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx"); String outFileName = getArtifactsDir() + "Document.NoSignatures.FromString.docx"; DigitalSignatureUtil.removeAllSignatures(doc.getOriginalFileName(), outFileName); // Remove all signatures from the document using stream parameters FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx"); FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.NoSignatures.FromInputStream.doc"); DigitalSignatureUtil.removeAllSignatures(streamIn, streamOut);
sign | |
public static void sign(java.io.InputStream srcStream, java.io.OutputStream dstStream, CertificateHolder certHolder) throws java.lang.Exception |
Document should be either
Output will be written to the start of stream and stream size will be updated with content length.
srcStream
- The stream which contains the document to sign.dstStream
- The stream that signed document will be written to.certHolder
- sign | |
public static void sign(java.io.InputStream srcStream, java.io.OutputStream dstStream, CertificateHolder certHolder, SignOptions signOptions) throws java.lang.Exception |
Document should be either
Output will be written to the start of stream and stream size will be updated with content length.
srcStream
- The stream which contains the document to sign.dstStream
- The stream that signed document will be written to.certHolder
- signOptions
- Example:
Shows how to sign encrypted document opened from a stream.FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.Encrypted.docx"); FileOutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.Encrypted.docx"); // Create certificate holder from a file. CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw"); SignOptions signOptions = new SignOptions(); signOptions.setComments("Comment"); signOptions.setSignTime(new Date()); signOptions.setDecryptionPassword("docPassword"); // Digitally sign encrypted with "docPassword" document in the specified path. DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);
sign | |
public static void sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder) throws java.lang.Exception |
Document should be either
srcFileName
- The file name of the document to sign.dstFileName
- The file name of the signed document output.certHolder
- sign | |
public static void sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder, SignOptions signOptions) throws java.lang.Exception |
Document should be either
srcFileName
- The file name of the document to sign.dstFileName
- The file name of the signed document output.certHolder
- signOptions
- Example:
Demonstrates how to add new signature line to the document and sign it with personal signature using SignatureLineId.public static void signSignatureLineUsingSignatureLineId() throws Exception { String signPersonName = "Ron Williams"; String srcDocumentPath = getMyDir() + "Document.docx"; String dstDocumentPath = getArtifactsDir() + "Document.Signed.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. } /// <summary> /// Signs the document obtained at the source location and saves it to the specified destination. /// </summary> 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); } /// <summary> /// Create test data that contains info about sing persons /// </summary> private static void createSignPersonData() throws IOException { InputStream inputStream = new FileInputStream(getImageDir() + "LogoSmall.png"); 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<SignPersonTestClass> gSignPersonList;
Example:
Shows how to sign documents using certificate holder and sign options.CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw"); // By string: Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx"); String outputFileName = getArtifactsDir() + "Document.DigitalSignature.docx"; SignOptions signOptions = new SignOptions(); signOptions.setComments("My comment"); signOptions.setSignTime(new Date()); DigitalSignatureUtil.sign(doc.getOriginalFileName(), outputFileName, certificateHolder, signOptions); // By stream: InputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx"); OutputStream streamOut = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx"); DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);