How to Restrict Editing on Word Documents in Java
Tags: word, java, edit, restriction
Published: 2022-10-28 07:56:13


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](https://www.e-iceblue.com/Introduce/doc-for-java.html) offers **ProtectionType** enumeration parameters to allow users to select an exact type of protection in the following aspects:

- [Set the Document Read Only] (#set-the-document-read-only)
- [Only Allow Comments on Word] (#only-allow-comments-on-word)
- [Only Fill in Forms on Word] (#only-fill-in-forms-on-word)
- [Only Allow Revisions] (#protect-the-documents-from-unintentional-editing-with-all-changes-be-tracked)
- [No Protection on Word] (#turn-off-editing-restrictions)

###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.

```java
<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](https://www.e-iceblue.com/Download/doc-for-java.html), 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.

```java
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);
    }
}
```

![Read Only](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xq2oi0mmb2cnudvxwys4.png)
###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.

```java
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);
    }
}
```

![Comments Only](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/72wkkqi485xuw2556khs.png)

###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.

```java
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);
    }
}
```

![Form Fields Only](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2a6ycrlsfvd57r0las1e.png)

###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.

```java
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);
    }
}
```

![Track Changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jf3uu76meezrud908hr.png)

###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.

```java
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);
    }
}

```

![Turn Off restriction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt2c0n92u54trxfte0l9.png)

###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](https://www.e-iceblue.com/Tutorials/Java/Spire.Doc-for-Java/Program-Guide/Security/Encrypt-and-Decrypt-Word-Document-in-Java.html) to protect the Word documents. You can check [Word forum](url) for more details.