Digital Signatures

Overview

This guide shows how to read and remove the digital signatures of a Word file with Aspose.Words FOSS for Java. DigitalSignatureUtil provides methods for working with the digital signatures of a document file: loadSignatures loads them into a read-only DigitalSignatureCollection and removeAllSignatures writes an unsigned copy of a file. Creating a signature is not supported in this edition: DigitalSignatureUtil.sign throws an UnsupportedOperationException whose message is FOSS. In tests with a real certificate on Java 8, 17 and 21 the call was rejected every time. The example below was compiled and run against the published package on Java 8, 17 and 21.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe examples are statements. Put them in a method that declares throws Exception.

Count and Remove Signatures

The example saves an unsigned document, loads its signatures and prints how many there are, then writes a copy without signatures and counts again. Both counts printed 0, because the document was never signed. A signed file that you receive from somewhere else is handled by the same two calls.

import com.aspose.words.*;

Document doc = new Document();
new DocumentBuilder(doc).writeln("An unsigned document.");
doc.save("unsigned.docx");

// Reading: how many digital signatures does a file carry?
DigitalSignatureCollection signatures = DigitalSignatureUtil.loadSignatures("unsigned.docx");
System.out.println("Signatures in unsigned.docx: " + signatures.getCount());

// Removing: write a copy with every signature taken out.
DigitalSignatureUtil.removeAllSignatures("unsigned.docx", "copy.docx");
System.out.println("Signatures in copy.docx: " + DigitalSignatureUtil.loadSignatures("copy.docx").getCount());

The program printed:

Signatures in unsigned.docx: 0
Signatures in copy.docx: 0

Tips and Best Practices

  • Keep the original file: removeAllSignatures writes a new file and leaves the source as it is, as long as the destination is a different path.
  • DigitalSignatureCollection is read-only; getCount gives the number of signatures.
  • Do not plan a workflow that creates signatures with this edition.

Common Issues

IssueCauseFix
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.
The compiler reports an unreported exception.The Document constructors, Document.save and some other methods declare throws Exception.Declare it on the calling method or catch it.
sign throws an UnsupportedOperationException with the message FOSS.Creating digital signatures is not supported in this edition.Sign the document with another tool after you save it.

FAQ

Can I sign a document?

No. sign is rejected in this edition.

Can I check whether a file is signed?

Yes. loadSignatures returns the collection and getCount tells how many signatures it has.

Which files can I use it on?

Word files such as DOCX. In tests on Java 8, 17 and 21 a Markdown file was rejected with an IllegalStateException saying that the signing feature is not supported by the file format.


API Reference Summary

Class / MethodDescription
DigitalSignatureUtilProvides methods for working with the digital signatures of a document file.
DigitalSignatureCollectionProvides a read-only collection of digital signatures attached to a document.
DigitalSignatureUtil.loadSignatures(fileName)Loads digital signatures from document.
DigitalSignatureUtil.removeAllSignatures(src, dst)Removes all digital signatures from source file and writes unsigned file to destination file.

Related Guides

See Also