When you share a word document with others, you may want to restrict editing on a part or all of the word document to protect the information on the word document. In this article, you will learn how to set the editing restrictions on Word document with the help of Spire.Doc for Java. Spire.Doc for Java offers ProtectionType enumeration parameters to allow users to select an exact type of protection in the following aspects:
- Set the Document Read Only
- Only Allow Comments on Word
- Only Fill in Forms on Word
- Only Allow Revisions
- No Protection on Word
Java Library to Restrict Editing on Word Documents
Method 1: If you are using 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>10.10.7</version>
</dependency>
</dependencies>
Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.
Set the document read only
It is quite easy to protect a Word document with password using Spire.Doc for Java. Reads can view the information on Word document but canโt make any changes on it.
- Create a Document instance and load a sample Word document using Document.loadFromFile() method.
- Use Document.protect(ProtectionType. Allow_Only_Reading, password string) method to make the Word document read-only.
- Save the document to another word using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;
public class protectWord {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();
//Load the Word document
document.loadFromFile("Sample.docx");
//ReadOnly
document.protect(ProtectionType.Allow_Only_Reading, "123456");
//Save the document to file
document.saveToFile("ReadOnly.docx", FileFormat.Docx_2013);
}
}
Only Allow Comments on Word
The step details of only allow comments to be inserted on word document.
- Create a Document instance and load a sample Word document using Document.loadFromFile() method.
- Use Document.protect(ProtectionType. Allow_Only_Comments, password string) method to make the Word document only the comments can be added.
- Save the document to another word using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;
public class protectWord {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();
//Load the Word document
document.loadFromFile("Sample.docx");
//Allow only comments
document.protect(ProtectionType.Allow_Only_Comments, "123456");
//Save the document to file
document.saveToFile("CommentsOnly.docx", FileFormat.Docx_2013);
}
}
Only Fill in Forms on Word
Only the forms on the word documents can be filled without any other changes.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Use Document.protect(ProtectionType. Allow_Only_Comments, password string) method protect the word document only can be edited by filling in forms.
- Save the document to another word using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;
public class protectWord {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();
//Load the Word document
document.loadFromFile("Sample.docx");
//Allow only comments
document.protect(ProtectionType.Allow_Only_Form_Fields, "123456");
//Save the document to file
document.saveToFile("FormFieldsOnly.docx", FileFormat.Docx_2013);
}
}
Protect the Documents from Unintentional Editing with All Changes be Tracked
When the ProtectionType is set as Allow_Only_Revisions, word documents can be edited with all the changes be tracked. It is very easy to check the editing records on the word document. You can accept or reject the changes later.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Use Document.protect(ProtectionType.Allow_Only_Revisions, password string) method to ensure the word document be edited under track changes.
- Save the document to another word using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;
public class protectWord {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();
//Load the Word document
document.loadFromFile("Sample.docx");
//Allow only comments
document.protect(ProtectionType.Allow_Only_Revisions, "123456");
//Save the document to file
document.saveToFile("TrackChanges.docx", FileFormat.Docx_2013);
}
}
Turn off editing restrictions
The final section shows how to turn off editing restrictions. After you set the ProtectionType as No_Protection, the edit restriction on the word document will be removed. And then the word document can be edited as usual.
- Create a Document instance.
- Load a sample Word document using Document.loadFromFile() method.
- Use Document.protect(ProtectionType.No_Protection, password string) method to remove the edit restriction.
- Save the document to another word using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;
public class protectWord {
public static void main(String[] args) throws Exception {
//Create a Document instance
Document document = new Document();
//Load the Word document
document.loadFromFile("ReadOnly.docx");
//Allow only comments
document.protect(ProtectionType.No_Protection,"123456");
//Save the document to file
document.saveToFile("NoProtection.docx", FileFormat.Docx_2013);
}
}
Conclusion
In this article, you have learned how to restrict the editing of Word documents and turn off the editing restrictions using different protection types. Spire.Doc for Java also offers many other security functions to protect the Word documents. You can check Word forum for more details.
Top comments (0)