com.aspose.words

Class DigitalSignatureCollection

  • java.lang.Object
    • com.aspose.words.DigitalSignatureCollection
  • All Implemented Interfaces:
    java.lang.Iterable
    public class DigitalSignatureCollection 
    extends java.lang.Object

Provides a read-only collection of digital signatures attached to a document.
Document.DigitalSignatures

Example:

Shows how to validate each signature in a document and display basic information about the signature.
// Load the document which contains signature
Document doc = new Document(getMyDir() + "Digitally signed.docx");

for (DigitalSignature signature : doc.getDigitalSignatures()) {
    System.out.println("*** Signature Found ***");
    System.out.println("Is valid: " + signature.isValid());
    // This property is available in MS Word documents only
    System.out.println("Reason for signing: " + signature.getComments());
    System.out.println("Signature type: " + signature.getSignatureType());
    System.out.println("Time of signing: " + signature.getSignTime());
    System.out.println("Subject name: " + signature.getSubjectName());
    System.out.println("Issuer name: " + signature.getIssuerName());
    System.out.println();
}

Example:

Shows how to sign documents with X.509 certificates.
// Verify that a document isn't signed
Assert.assertFalse(FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx").hasDigitalSignature());

// Create a CertificateHolder object from a PKCS #12 file, which we will use to sign the document
CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null);

// There are 2 ways of saving a signed copy of a document to the local file system
// 1: Designate unsigned input and signed output files by filename and sign with the passed CertificateHolder
SignOptions signOptions = new SignOptions();
signOptions.setSignTime(new Date());

DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "Document.DigitalSignature.docx",
        certificateHolder, signOptions);

// 2: Create a stream for the input file and one for the output and create a file, signed with the CertificateHolder, at the file system location determine
InputStream inDoc = new FileInputStream(getMyDir() + "Document.docx");
try {
    OutputStream outDoc = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");
    try {
        DigitalSignatureUtil.sign(inDoc, outDoc, certificateHolder);
    } finally {
        if (outDoc != null) outDoc.close();
    }
} finally {
    if (inDoc != null) inDoc.close();
}

Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature());

// Open the signed document and get its digital signature collection
Document signedDoc = new Document(getArtifactsDir() + "Document.DigitalSignature.docx");
DigitalSignatureCollection digitalSignatureCollection = signedDoc.getDigitalSignatures();

// Verify that all of the document's digital signatures are valid and check their details
Assert.assertTrue(digitalSignatureCollection.isValid());
Assert.assertEquals(1, digitalSignatureCollection.getCount());
Assert.assertEquals(DigitalSignatureType.XML_DSIG, digitalSignatureCollection.get(0).getSignatureType());
Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getIssuerName());
Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getSubjectName());

Constructor Summary
 
Property Getters/Setters Summary
intgetCount()
Gets the number of elements contained in the collection.
booleanisValid()
Returns true if all digital signatures in this collection are valid and the document has not been tampered with Also returns true if there are no digital signatures. Returns false if at least one digital signature is invalid.
DigitalSignatureget(int index)
Gets a document signature at the specified index.
 
Method Summary
java.util.Iterator<DigitalSignature>iterator()
Returns a dictionary iterator object that can be used to iterate over all items in the collection.
 

    • Constructor Detail

      • DigitalSignatureCollection

        public DigitalSignatureCollection()
    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the number of elements contained in the collection.

        Example:

        Shows how to sign documents with X.509 certificates.
        // Verify that a document isn't signed
        Assert.assertFalse(FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx").hasDigitalSignature());
        
        // Create a CertificateHolder object from a PKCS #12 file, which we will use to sign the document
        CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null);
        
        // There are 2 ways of saving a signed copy of a document to the local file system
        // 1: Designate unsigned input and signed output files by filename and sign with the passed CertificateHolder
        SignOptions signOptions = new SignOptions();
        signOptions.setSignTime(new Date());
        
        DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "Document.DigitalSignature.docx",
                certificateHolder, signOptions);
        
        // 2: Create a stream for the input file and one for the output and create a file, signed with the CertificateHolder, at the file system location determine
        InputStream inDoc = new FileInputStream(getMyDir() + "Document.docx");
        try {
            OutputStream outDoc = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");
            try {
                DigitalSignatureUtil.sign(inDoc, outDoc, certificateHolder);
            } finally {
                if (outDoc != null) outDoc.close();
            }
        } finally {
            if (inDoc != null) inDoc.close();
        }
        
        Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature());
        
        // Open the signed document and get its digital signature collection
        Document signedDoc = new Document(getArtifactsDir() + "Document.DigitalSignature.docx");
        DigitalSignatureCollection digitalSignatureCollection = signedDoc.getDigitalSignatures();
        
        // Verify that all of the document's digital signatures are valid and check their details
        Assert.assertTrue(digitalSignatureCollection.isValid());
        Assert.assertEquals(1, digitalSignatureCollection.getCount());
        Assert.assertEquals(DigitalSignatureType.XML_DSIG, digitalSignatureCollection.get(0).getSignatureType());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getIssuerName());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getSubjectName());
      • isValid

        public boolean isValid()
        
        Returns true if all digital signatures in this collection are valid and the document has not been tampered with Also returns true if there are no digital signatures. Returns false if at least one digital signature is invalid.

        Example:

        Shows how to sign documents with X.509 certificates.
        // Verify that a document isn't signed
        Assert.assertFalse(FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx").hasDigitalSignature());
        
        // Create a CertificateHolder object from a PKCS #12 file, which we will use to sign the document
        CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null);
        
        // There are 2 ways of saving a signed copy of a document to the local file system
        // 1: Designate unsigned input and signed output files by filename and sign with the passed CertificateHolder
        SignOptions signOptions = new SignOptions();
        signOptions.setSignTime(new Date());
        
        DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "Document.DigitalSignature.docx",
                certificateHolder, signOptions);
        
        // 2: Create a stream for the input file and one for the output and create a file, signed with the CertificateHolder, at the file system location determine
        InputStream inDoc = new FileInputStream(getMyDir() + "Document.docx");
        try {
            OutputStream outDoc = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");
            try {
                DigitalSignatureUtil.sign(inDoc, outDoc, certificateHolder);
            } finally {
                if (outDoc != null) outDoc.close();
            }
        } finally {
            if (inDoc != null) inDoc.close();
        }
        
        Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature());
        
        // Open the signed document and get its digital signature collection
        Document signedDoc = new Document(getArtifactsDir() + "Document.DigitalSignature.docx");
        DigitalSignatureCollection digitalSignatureCollection = signedDoc.getDigitalSignatures();
        
        // Verify that all of the document's digital signatures are valid and check their details
        Assert.assertTrue(digitalSignatureCollection.isValid());
        Assert.assertEquals(1, digitalSignatureCollection.getCount());
        Assert.assertEquals(DigitalSignatureType.XML_DSIG, digitalSignatureCollection.get(0).getSignatureType());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getIssuerName());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getSubjectName());
      • get

        public DigitalSignature get(int index)
        
        Gets a document signature at the specified index.
        Parameters:
        index - Zero-based index of the signature.

        Example:

        Shows how to sign documents with X.509 certificates.
        // Verify that a document isn't signed
        Assert.assertFalse(FileFormatUtil.detectFileFormat(getMyDir() + "Document.docx").hasDigitalSignature());
        
        // Create a CertificateHolder object from a PKCS #12 file, which we will use to sign the document
        CertificateHolder certificateHolder = CertificateHolder.create(getMyDir() + "morzal.pfx", "aw", null);
        
        // There are 2 ways of saving a signed copy of a document to the local file system
        // 1: Designate unsigned input and signed output files by filename and sign with the passed CertificateHolder
        SignOptions signOptions = new SignOptions();
        signOptions.setSignTime(new Date());
        
        DigitalSignatureUtil.sign(getMyDir() + "Document.docx", getArtifactsDir() + "Document.DigitalSignature.docx",
                certificateHolder, signOptions);
        
        // 2: Create a stream for the input file and one for the output and create a file, signed with the CertificateHolder, at the file system location determine
        InputStream inDoc = new FileInputStream(getMyDir() + "Document.docx");
        try {
            OutputStream outDoc = new FileOutputStream(getArtifactsDir() + "Document.DigitalSignature.docx");
            try {
                DigitalSignatureUtil.sign(inDoc, outDoc, certificateHolder);
            } finally {
                if (outDoc != null) outDoc.close();
            }
        } finally {
            if (inDoc != null) inDoc.close();
        }
        
        Assert.assertTrue(FileFormatUtil.detectFileFormat(getArtifactsDir() + "Document.DigitalSignature.docx").hasDigitalSignature());
        
        // Open the signed document and get its digital signature collection
        Document signedDoc = new Document(getArtifactsDir() + "Document.DigitalSignature.docx");
        DigitalSignatureCollection digitalSignatureCollection = signedDoc.getDigitalSignatures();
        
        // Verify that all of the document's digital signatures are valid and check their details
        Assert.assertTrue(digitalSignatureCollection.isValid());
        Assert.assertEquals(1, digitalSignatureCollection.getCount());
        Assert.assertEquals(DigitalSignatureType.XML_DSIG, digitalSignatureCollection.get(0).getSignatureType());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getIssuerName());
        Assert.assertEquals("CN=Morzal.Me", signedDoc.getDigitalSignatures().get(0).getSubjectName());
    • Method Detail

      • iterator

        public java.util.Iterator<DigitalSignature> iterator()
        Returns a dictionary iterator object that can be used to iterate over all items in the collection.

        Example:

        Shows how to load and enumerate all digital signatures of a document.
        DigitalSignatureCollection digitalSignatures =
                DigitalSignatureUtil.loadSignatures(getMyDir() + "Digitally signed.docx");
        
        Iterator<DigitalSignature> enumerator = digitalSignatures.iterator();
        while (enumerator.hasNext()) {
            // Do something useful
            DigitalSignature ds = enumerator.next();
        
            if (ds != null)
                System.out.println(ds.toString());
        }