How to Check and Remove Digital Signatures of a Word Document in Java
Aspose.Words FOSS for Java reads and removes the digital signatures of a Word file with DigitalSignatureUtil. This guide counts the signatures of a file and writes an unsigned copy, with the library from Maven Central. Creating a signature is not supported.
Step-by-Step Guide
Step 1: Install the Package
Add the package to your build. The library needs Java 8 or later.
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-words-foss</artifactId>
<version>26.3.0</version>
</dependency>Step 2: Import the Classes
All classes are in the com.aspose.words package, so the example starts with import com.aspose.words.*;.
Step 3: Load the Signatures
DigitalSignatureUtil.loadSignatures(fileName) returns a DigitalSignatureCollection; getCount returned 0 for the unsigned file in the example.
Step 4: Write an Unsigned Copy
removeAllSignatures(source, destination) writes a copy without signatures. The copy also counted 0, because the example uses an unsigned file.
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: 0Common Issues and Fixes
sign throws an UnsupportedOperationException. The message is FOSS. Creating a digital signature is not supported in this edition.
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.
Frequently Asked Questions
Can I add a signature?
No.
Does removeAllSignatures change the source file?
Not when the destination is a different path: it writes a new file. If you pass the same path as source and destination, the source file is overwritten.