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.

Password Protect Word Documents in Java

The following topics are covered below:

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.

	<name>GroupDocs Java API</name>

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

Programmatically Locked Word Doc

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.

Now, whenever you try to open the password-protected document, the document viewer & editor asks for the password to open the file.

Enter Password to Open Protected Word Document

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.

The following code snippet changes the current password of the Word document using Java.

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.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.


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.

See Also