com.aspose.words

Class DigitalSignatureUtil

  • java.lang.Object
    • com.aspose.words.DigitalSignatureUtil
public abstract class DigitalSignatureUtil 
extends java.lang.Object

Provides methods for signing document.

Since digital signature works with file content rather than Document Object Model these methods are put into a separate class.

Supported formats are LoadFormat.DOC and LoadFormat.DOCX.

Example:

Shows how to remove every signature from a document.
// By string:
Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
String outFileName = getMyDir() + "\\Artifacts\\Document.NoSignatures.FromString.docx";

DigitalSignatureUtil.removeAllSignatures(doc.getOriginalFileName(), outFileName);

// By stream:
FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
FileOutputStream streamOut = new FileOutputStream(getMyDir() + "\\Artifacts\\Document.NoSignatures.FromInputStream.doc");

DigitalSignatureUtil.removeAllSignatures(streamIn, streamOut);

Method Summary
static DigitalSignatureCollectionloadSignatures(java.lang.String fileName)
Loads digital signatures from document.
static voidremoveAllSignatures(java.lang.String srcFileName, java.lang.String dstFileName)
Removes all digital signatures from source file and writes unsigned file to destination file.
static voidsign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder)
Signs source document using given CertificateHolder with digital signature and writes signed document to destination file.

Document should be either LoadFormat.DOC or LoadFormat.DOCX.

static voidsign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder, SignOptions signOptions)
Signs source document using given CertificateHolder and SignOptions with digital signature and writes signed document to destination file.

Document should be either LoadFormat.DOC or LoadFormat.DOCX.

 

    • Method Detail

      • loadSignatures

        public static DigitalSignatureCollection loadSignatures(java.lang.String fileName)
                                                             throws java.lang.Exception
        Loads digital signatures from document.
        Parameters:
        fileName - Path to the document.
        Returns:
        Collection of digital signatures. Returns empty collection if file is not signed.

        Example:

        Shows how to load all existing signatures from a document.
        // By string:
        DigitalSignatureCollection digitalSignatures = DigitalSignatureUtil.loadSignatures(getMyDir() + "Document.DigitalSignature.docx");
        
        // By stream:
        InputStream stream = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
        
        digitalSignatures = DigitalSignatureUtil.loadSignatures(stream);
      • removeAllSignatures

        public static void removeAllSignatures(java.lang.String srcFileName, java.lang.String dstFileName)
                                            throws java.lang.Exception
        Removes all digital signatures from source file and writes unsigned file to destination file.

        Example:

        Shows how to remove every signature from a document.
        // By string:
        Document doc = new Document(getMyDir() + "Document.DigitalSignature.docx");
        String outFileName = getMyDir() + "\\Artifacts\\Document.NoSignatures.FromString.docx";
        
        DigitalSignatureUtil.removeAllSignatures(doc.getOriginalFileName(), outFileName);
        
        // By stream:
        FileInputStream streamIn = new FileInputStream(getMyDir() + "Document.DigitalSignature.docx");
        FileOutputStream streamOut = new FileOutputStream(getMyDir() + "\\Artifacts\\Document.NoSignatures.FromInputStream.doc");
        
        DigitalSignatureUtil.removeAllSignatures(streamIn, streamOut);
      • sign

        public static void sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder)
                             throws java.lang.Exception
        Signs source document using given CertificateHolder with digital signature and writes signed document to destination file.

        Document should be either LoadFormat.DOC or LoadFormat.DOCX.

        Parameters:
        srcFileName - The file name of the document to sign.
        dstFileName - The file name of the signed document output.
        certHolder - CertificateHolder object with certificate that used to sign file.
      • sign

        public static void sign(java.lang.String srcFileName, java.lang.String dstFileName, CertificateHolder certHolder, SignOptions signOptions)
                             throws java.lang.Exception
        Signs source document using given CertificateHolder and SignOptions with digital signature and writes signed document to destination file.

        Document should be either LoadFormat.DOC or LoadFormat.DOCX.

        Parameters:
        srcFileName - The file name of the document to sign.
        dstFileName - The file name of the signed document output.
        certHolder - CertificateHolder object with certificate that used to sign file.
        signOptions - SignOptions object with various signing options.

        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 = getMyDir() + "\\Artifacts\\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(getMyDir() + "\\Artifacts\\Document.DigitalSignature.docx");
        
        DigitalSignatureUtil.sign(streamIn, streamOut, certificateHolder, signOptions);

        Example:

        Shows how to sign encrypted document opened from a file.
        String outputFileName = getMyDir() + "\\Artifacts\\Document.Encrypted.docx";
        
        Document doc = new Document(getMyDir() + "Document.Encrypted.docx", new LoadOptions("docPassword"));
        
        // 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(doc.getOriginalFileName(), outputFileName, certificateHolder, signOptions);