How to Protect and Encrypt a Word Document with a Password in Java
Aspose.Words FOSS for Java can mark a Word document as protected and can encrypt the saved DOCX file with a password. This guide encrypts a file, shows what happens when it is opened without the password or with a wrong one, and opens it with the right password, with the library from Maven Central.
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: Set the Password in the Save Options
Create an OoxmlSaveOptions, call setPassword and pass it to doc.save(file, saveOptions). FileFormatUtil.detectFileFormat(file).isEncrypted() printed true for the saved file and false for a DOCX saved without options.
Step 4: Handle a Missing or Wrong Password
new Document(file) and new Document(file, new LoadOptions("wrong")) both threw IncorrectPasswordException with the message The document password is incorrect. in a test on Java 8, 17 and 21. Catch it where the file is opened.
Step 5: Open the File With the Password
new Document(file, new LoadOptions(password)) opened the file and getText returned the text that was written.
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Confidential text");
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setPassword("open-sesame");
doc.save("encrypted.docx", saveOptions);
doc.save("plain.docx");
System.out.println("plain.docx encrypted: " + FileFormatUtil.detectFileFormat("plain.docx").isEncrypted());
System.out.println("encrypted.docx encrypted: " + FileFormatUtil.detectFileFormat("encrypted.docx").isEncrypted());
try {
new Document("encrypted.docx");
} catch (IncorrectPasswordException e) {
System.out.println("No password: " + e.getMessage());
}
try {
new Document("encrypted.docx", new LoadOptions("wrong"));
} catch (IncorrectPasswordException e) {
System.out.println("Wrong password: " + e.getMessage());
}
Document opened = new Document("encrypted.docx", new LoadOptions("open-sesame"));
System.out.println("Right password: " + opened.getText().trim());The program printed:
plain.docx encrypted: false
encrypted.docx encrypted: true
No password: The document password is incorrect.
Wrong password: The document password is incorrect.
Right password: Confidential textStep 6: Protect Against Editing Instead
doc.protect(ProtectionType.READ_ONLY, password) marks the document as protected and the setting is saved with the file. The Protection guide lists the four protection types and shows unprotect.
Common Issues and Fixes
IncorrectPasswordException is thrown when the file is opened. The file is encrypted and the password is missing, wrong or differs in case; the message was The document password is incorrect. Pass the password with new LoadOptions(password).
A re-saved document is no longer encrypted. The password belongs to the save options, not to the document. A document opened with its password and saved with doc.save(file) was written as an ordinary DOCX in a probe, so pass the OoxmlSaveOptions with the password again.
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
Is protecting a document the same as encrypting it?
No. protect saves a protection type in the document settings and the file still opens without a password; encryption makes the file need a password to open.
Can I check a file before opening it?
Yes. FileFormatUtil.detectFileFormat(file).isEncrypted() returns true for an encrypted file.
See Also
- Protection and Encryption (docs)
- OoxmlSaveOptions class reference
- LoadOptions class reference
- ProtectionType class reference
- How to Work with Documents in Java
- How to Check and Remove Digital Signatures of a Word Document in Java
- How to Set Custom Properties on a Word Document in Java
- Aspose.Words for Java — Enterprise Knowledge Base