Protect or Unprotect DOCX in Java

MS Word allows you to protect the DOCX document with various mechanisms. You can set a password that is required to open the document. On the other hand, you can specify the protection levels such as making the document read-only, allowing comments or form fields only, and etc. In this article, you are going to learn how to automate the MS Word protection features in order to protect or unprotect DOCX files in Java-based applications.

Java API to Protect or Unprotect Word Files - Free Download

Aspose.Words for Java is a powerful word processing API that lets you create and process MS Word documents from within your Java-based applications. In addition, it allows automating the security features in order to protect or unprotect Word DOCX documents. You can either download the API or get it installed in your Maven-based application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <type>pom</type>
</dependency>

Protect Word DOCX File with Password in Java

The following are the simple steps to protect a Word DOCX document using a password.

The following code samples shows how to protect a Word DOCX document using Java.

Protect DOCX Files with Different Protection Types

You can also restrict the user’s access to the document with different protection levels. In this case, the user will only be authorized to perform a certain set of operations. Aspose.Words for Java provides the following protection types:

  • AllowOnlyComments – To allow modification of comments only.
  • AllowOnlyFormFields – To allow data entry into the form fields only.
  • AllowOnlyRevisions – To allow adding revision marks only.
  • ReadOnly – Completely read-only (no changes are allowed to the document).
  • NoProtection – No protection at all.

The following code sample shows how to apply a particular protection type in DOCX without a password.

Unprotect/Unlock DOCX Files using Java

Aspose.Words for Java lets you unprotect or unlock MS Word DOCX files in a couple of lines of code. Not only this, but you can unprotect the files without providing the password. The following are the steps to unlock a protected DOCX file.

The following code sample shows how to unprotect a Word DOCX file using Java.

Conclusion

In this article, you have learned how to protect or unprotect MS Word DOCX files using Java. Furthermore, you have seen how different protection types can be applied to a DOCX file. You can explore more about Aspose.Words for Java using documentation.

See Also