News Category

Java: Protect or Unprotect Word Documents

2022-11-09 01:56:00 Written by  support iceblue
Rate this item
(0 votes)

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.

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.3.1</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);
    }
}

Java: Protect or Unprotect Word Documents

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");
    }
}

Java: Protect or Unprotect Word Documents

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");
    }
}

Java: Protect or Unprotect Word Documents

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");
    }
}

Java: Protect or Unprotect Word Documents

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.

Additional Info

  • tutorial_title:
Last modified on Monday, 12 June 2023 02:15