DEV Community

CodeSharing
CodeSharing

Posted on • Edited on

3 3

Set Editing Restrictions in Word using a free Java API

In order to protect a Word document, except the general method of encrypt the entire document with passward, we could also set editing restrictions to prevent the content from being modified by others.This article will demonstrate how to set editing restrictions in Word document using a Free Java API.

1# Installation
Method 1: Download the free API (Free Spire.Doc for Java) and unzip it, then add the Spire.Doc.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>3.9.0</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

2# Relevant Code Snippet:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ProtectionType;

public class RestrictEditing {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();
        //Load the Word document
        document.loadFromFile("Input 1.docx");

        //No changes (Read only)
        document.protect(ProtectionType.Allow_Only_Reading, "123456");

        //Allow only comments
        //document.protect(ProtectionType.Allow_Only_Comments, "123456");

        //Allow only filling in forms
        //document.protect(ProtectionType.Allow_Only_Form_Fields, "123456");

        //Allow only tracked changes
        //document.protect(ProtectionType.Allow_Only_Revisions, "123456");

        //Save the result document
        document.saveToFile("RestrictEditing.docx", FileFormat.Docx_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

3# A screenshot of the output Word document after setting editing restrictions as no changes (read only):
Alt Text

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs