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 Documents
- Protect Word DOCX Files with Password in Java
- Secure DOCX Files with Different Protection Types
- Unprotect/Unlock DOCX Files using Java
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.
- Load the DOCX file using Document class.
- Protect the Word document using Document.protect(int, java.lang.String) method.
- Save the protected document using the Document.save() method.
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.
- Load the DOCX file using Document class.
- Unprotect DOCX file using Document.unprotect() method.
- Save the unlocked file using the Document.save() method.
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.