Verify If a Word Document is Password Protected in Java
This article demonstrates how to verify if a Word document is password protected or not using Spire.Doc for Java.
The following image shows that the input Word document is protected with password:
import com.spire.doc.Document; public class DetectIfWordIsPasswordProtected { public static void main(String []args){ //Detect if the Word document is password protected boolean isPasswordProtected = Document.isPassWordProtected("C:\\Users\\Administrator\\Desktop\\Sample.docx"); if(isPasswordProtected) { System.out.println("The document is password protected."); } else { System.out.println("The document is not password protected."); } } }
Output:
Java: Digitally Sign a Word Document
A digital signature is a specific type of signature that is backed by a digital certificate, providing proof of your identity. Digital signatures are used to protect documents in a wide range of fields such as life insurance, invoices, rental agreements, and employment contracts and so on. This article will show you how to digitally sign a Word document in Java using Spire.Doc for Java.
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Add a Digital Signature to a Word Document in Java
The follows are the steps to digitally sign a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Specify the path and the password of a PFX certificate.
- Digitally sign the document while saving the document using Document.saveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword) method. There are some other methods that you can use to digitally sign a Word document.
- public void saveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, string securePassword);
- public void saveToStream(Stream stream, FileFormat fileFormat, byte[] certificateData, string securePassword);
- public void saveToStream(Stream stream, FileFormat fileFormat, string certificatePath, string securePassword);
- public static byte[] Document.sign(Stream sourceStream, byte[] certificateData, string securePassword);
- public static byte[] Document.sign(Stream sourceStream, string certificatePath, string securePassword);
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class DigitallySignWordDocument { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load a Word file doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Specify the certificate path String certificatePath = "C:\\Users\\Administrator\\Desktop\\certificate.pfx"; //Specify the password of the certificate String password = "e-iceblue"; //Digitally sign the document while saving it to a .docx file doc.saveToFile("AddDigitalSignature.docx", FileFormat.Docx_2013, certificatePath, password); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Verify the Restrict Editing Password of a Word Document in Java
This article demonstrates how to verify if the restrict editing password of a Word document is correct using Spire.Doc for Java.
import com.spire.doc.Document; public class VerifyRestrictEditingPassword { public static void main(String[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.loadFromFile("Input.docx"); //Verify if the password is valid boolean result = document.checkProtectionPassWord("123"); if(!result) { System.out.println("Oops! The password is invalid."); } else { System.out.println("Congrats! The password is valid."); } } }
Output:
Java: Set or Unset Editing Restrictions on Word Documents
Word document editing restrictions are password-protected limitations set on a Word document to restrict the type of editing that can be performed on the document. By setting restrictions on Word documents, users can exercise control over who can modify their documents and to what extent. This ensures that the important information remains unchanged while enabling smooth collaboration among multiple contributors and efficient information collection from a large number of people. On the other hand, it is also important to unsetting the editing restrictions to correct errors, update information, or accommodate changes. This article shows how to set or unset editing restrictions on Word documents using Spire.Doc for Java through Java programs.
- Set Editing Restrictions on Word Documents
- Add Exceptions While Setting Editing Restrictions on Word Documents
- Remove Editing Restrictions from Word Documents
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Set Editing Restrictions on Word Documents
Spire.Doc for Java supports setting four types of editing restrictions with a password on Word documents: no changes (read only), tracked changes, comments, and filling in forms. These editing restrictions can be set by using the Document.protect() method and some Enums.
Here is a list of the Enums to set the editing restrictions and their descriptions.
Enum | Restriction | Description |
ProtectionType.Allow_Only_Reading | No changes (read only) | Allow reading only. |
ProtectionType.Allow_Only_Revisions | Tracked changes | Allow tracked changes only. |
ProtectionType.Allow_Only_Comments | Comments | Allow commenting only. |
ProtectionType.Allow_Only_Form_Fields | Filling in forms | Allow filling in forms only. |
ProtectionType.No_Protection | No restriction | Allow any editing. |
The detailed step for setting editing restrictions with a password on a Word document are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Set the editing restrictions with a password using Document.protect() method.
- Save the document using Document.saveToFIle() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.ProtectionType; public class setEditingRestriction { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Set the restriction type to read-only and add a password doc.protect(ProtectionType.Allow_Only_Reading, "password"); //Set the restriction type to comment-only and add a password //doc.protect(ProtectionType.Allow_Only_Comments, "password"); //Set the restriction type to form-filling-only and add a password //doc.protect(ProtectionType.Allow_Only_Form_Fields, "password"); //Set the restriction type to revision-only and add a password //doc.protect(ProtectionType.Allow_Only_Revisions, "password"); //Save the document doc.saveToFile("EditingRestrictions.docx", FileFormat.Auto); } }
Add Exceptions While Setting Editing Restrictions on Word Documents
Users can add exceptions (unrestricted areas) when setting editing restrictions on Word documents by inserting permission starting and ending tags. The details steps are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Create an object of PermissionStart class and an object of PermissionEnd class.
- Get the first section using Document.getSections().get() method.
- Insert the permission starting and ending tags to the paragraphs to set unrestricted areas.
- Set editing restrictions with a password on the other areas using Document.protect() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; public class setRegionalEditingRestrictions { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Create the permission starting and ending tags PermissionStart start = new PermissionStart(doc, "permission1"); PermissionEnd end = new PermissionEnd(doc, "permission1"); //Get the first section Section section = doc.getSections().get(0); //Insert the permission starting tag and the permission ending tag to the document section.getParagraphs().get(0).getChildObjects().insert(0,start); section.getParagraphs().get(5).getChildObjects().add(end); //Set the editing restrictions with a password doc.protect(ProtectionType.Allow_Only_Reading, "password"); //Save the document doc.saveToFile("RestrictionExceptions.docx", FileFormat.Auto); } }
Remove Editing Restrictions from Word Documents
Editing restrictions can be removed by setting the editing restrictions to allow any editing. The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Set the editing restrictions to allow any editing and remove the password using Document.protect() method.
- If there are areas of exception, find the permission start tags and permission end tags and remove them.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class removeEditingRestriction { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("RegionalEditingRestrictions.docx"); //Remove the editing restrictions doc.protect(ProtectionType.No_Protection); //Find permission starting tags and permission ending tags and remove them for(int j=0;j<doc.getSections().getCount();j++){ //Get a section Section section=doc.getSections().get(j); for(int k=0;k<section.getParagraphs().getCount();k++){ //Get a paragraph in a section Paragraph paragraph=section.getParagraphs().get(k); for(int i=0;i<paragraph.getChildObjects().getCount();){ //Get a child object of a paragraph DocumentObject obj=paragraph.getChildObjects().get(i); //Determine if a child object is an instance of PermissionStart or PermissionEnd class if(obj instanceof PermissionStart||obj instanceof PermissionEnd){ //Remove the child object if it is paragraph.getChildObjects().remove(obj); }else{ i++; } } } } //Save the document doc.saveToFile("NoRestrictions.docx", FileFormat.Auto); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Java: Protect or Unprotect Word Documents
Enabling security options of your Word documents is essential for keeping sensitive information safe. You can encrypt your document with a password so that it cannot be opened by unauthorized users; you can enable the Read-only mode to prevent users from changing the content; you can also partially restrict editing your document. This article demonstrates how to protect or unprotect Word documents in Java using Spire.Doc for Java.
- Protect a Word Document with a Password in Java
- Change Permission of a Word Document in Java
- Lock Specified Sections of a Word Document in Java
- Mark a Word Document as Final in Java
- Remove Password from an Encrypted Word Document in Java
Install Spire.Doc for Java
First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>12.4.6</version> </dependency> </dependencies>
Protect a Word Document with a Password in Java
Encrypting a document with a password makes sure that only you and certain people can read or edit it. The following are the steps to password protect a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Encrypt the document with a password using Document.encrypt() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class PasswordProtectWord { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Load a Word file document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Encrypt the document with a password document.encrypt("open-psd"); //Save the document to another Word file document.saveToFile("output/Encryption.docx", FileFormat.Docx); } }
Change Permission of a Word Document in Java
Documents encrypted with an open password cannot be opened by those who do not know the password. If you’d like to grant people permission to read your document but restrict the types of modifications that someone can make, you can set the document permission. The following are the steps to change permission of a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Set the document permission and the permission password using Document.protect() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.ProtectionType; public class ChangePermission { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Load a Word document document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Set the document permission and set the permission password document.protect(ProtectionType.Allow_Only_Form_Fields, "permission-psd"); //Save the document to another Word file document.saveToFile("output/Permission.docx"); } }
Lock Specified Sections of a Word Document in Java
You can lock parts of your Word document so that they cannot be changed and leave the unlocked parts available for editing. The following are the steps to protect specified sections of a Word document using Spire.Doc for Java.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Set the editing restriction as Allow_Only_Form_Fields.
- Unprotect a specific section by passing false to Section.protectForm() as a parameter. The rest sections will continue to be protected.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.ProtectionType; public class LockSpecificSections { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load a Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Set editing restriction as "Allow_Only_Form_Fields" doc.protect(ProtectionType.Allow_Only_Form_Fields, "permissionPsd"); //Unprotect section 2 doc.getSections().get(1).setProtectForm(false); //Save the document to another Word file doc.saveToFile("output/ProtectSection.docx"); } }
Mark a Word Document as Final in Java
By marking a document as Final, you disable typing, editing, and format changes capabilities and a message will appear to any reader that the document has been finalized. The following are the steps to mark a Word document as final using Spire.Doc for Java.
- Create a Document object.
- Load a Word file using Document.loadFromFile() method.
- Get the CustomDocumentProperties object from the document.
- Add a custom property "_MarkAsFinal" to the document.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.CustomDocumentProperties; import com.spire.doc.Document; public class MarkAsFinal { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Load a Word document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx"); //Get custom document properties CustomDocumentProperties customProperties = doc.getCustomDocumentProperties(); //Add "_MarkAsFinal" as a property to the document customProperties.add("_MarkAsFinal", true); //Save the document to another Word file doc.saveToFile("output/MarkAsFinal.docx"); } }
Remove Password from an Encrypted Word Document in Java
You can remove the password from an encrypted document if the encryption is no longer needed. The following are the detailed steps.
- Create a Document object.
- Load a Word document using Document.loadFromFile() method.
- Remove the password using Document.removeEncryption() method.
- Save the document to another Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemovePassword { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Load an encrypted Word document document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Encryption.docx", FileFormat.Docx, "open-psd"); //Remove encryption document.removeEncryption(); //Save the document to another Word file document.saveToFile("output/RemoveEncryption.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.