Earlier we restricted the access to PDF documents by applying passwords. In this article, we will see how to password protect Word documents in Java. Further, we will also learn to change the existing password of DOC & DOCX files, and lastly, how to remove the password protection to unlock Word documents within the Java applications.

The following topics are covered below:
- Java API for to Lock/Unlock Word Documents
- Add Password to Word Document
- Change Password of Word Document
- How to Remove Password from Word Document
Java API to Lock/Unlock Word Documents
GroupDocs.Merger showcases the Java API that allows locking and unlocking of Word documents within the Java applications. We will use GroupDocs.Merger for Java to add the password to Word files, change it, and also remove password protection from the Word files in Java.
You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-merger</artifactId>
<version>21.9</version>
</dependency>
For more details about the API and about the other supported document formats, you can visit the documentation and the GitHub repository for the source code examples.
Password Protect Word Document in Java

Let’s start with adding a password to the MS Word DOCX file for security. The following steps show how to add a password to Word documents in Java.
- Define the password using AddPasswordOptions class.
- Load the DOCX file using the Merger class.
- Make it password-protected using addPassword() method.
- Save the protected file using the appropriate save() method.
The following Java code snippet adds a password to a MS Word DOCX file.
/* | |
* Password Protect Word Documents in Java | |
*/ | |
AddPasswordOptions addOptions = new AddPasswordOptions("mySECRETpassWORD"); | |
Merger merger = new Merger("path/document.docx"); | |
merger.addPassword(addOptions); | |
merger.save("path/protected-document.docx"); |
Now, whenever you try to open the password-protected document, the document viewer & editor asks for the password to open the file.

Change Existing Password of Word Document in Java
Let’s change the password with a new one. The following steps change the existing password of the Word file in Java.
- Set the loading options using the current password.
- Now define the new password using password update options.
- Load the protected Word document using Merger class and the defined loading options.
- Use updatePassword() method to apply the new password.
- Save the password protected file again using the save() method.
The following code snippet changes the current password of the Word document using Java.
/* | |
* Change password of the protected DOC/DOCX documents in Java | |
*/ | |
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD"); | |
UpdatePasswordOptions updateOptions = new UpdatePasswordOptions("TOPSECRET_pa22WORD"); | |
Merger merger = new Merger("path/protected-document.docx", loadOptions); | |
merger.updatePassword(updateOptions); | |
merger.save("path/pwd-changed-document.docx"); |
Remove Password from Word Document in Java
If the document is no more confidential and the file protection is not required, you can simply remove the password. The following steps show how to remove the password of a protected Word file in Java.
- Load the protected Word document using Merger class and the existing password.
- Remove its password using removePassword() method.
- Save the unlocked DOCX file using the save() method.
The following is the Java code example that removes the password of a Word file to make it unlocked/unprotected.
/* | |
* Remove password from Word document in Java | |
*/ | |
LoadOptions loadOptions = new LoadOptions("mySECRETpassWORD"); | |
Merger merger = new Merger("path/protected-document.docx", loadOptions); | |
merger.removePassword(); | |
merger.save("path/no-pwd-document.docx"); |
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
Let’s sum up what we discussed above. Using a simple Word document, we locked it with a password using Java example. Next, we learned to change the existing password. Lastly, we removed the password of the Word file to make it unlocked within any Java application.
To learn more about GroupDocs.Merger for Java, visit its documentation to start building your own document protector or password remover applications for various supported document formats. For queries, contact us via the forum.