How to Create Editable Ranges in a Protected Word Document in Java
Aspose.Words FOSS for Java marks editable parts of a document with startEditableRange and endEditableRange. This guide creates a range, protects the document and removes a range again, with the library from Maven Central.
Step-by-Step Guide
Step 1: Install the Package
Add the package to your build. The library needs Java 8 or later.
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-words-foss</artifactId>
<version>26.3.0</version>
</dependency>Step 2: Import the Classes
All classes are in the com.aspose.words package, so the example starts with import com.aspose.words.*;. The examples are statements: put them in a method that declares throws Exception.
Step 3: Create the Range and Protect the Document
Call builder.startEditableRange(), write the text that may change, call builder.endEditableRange() and then doc.protect(ProtectionType.READ_ONLY, password).
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("This sentence stays locked.");
builder.write("Fill in your name: ");
EditableRangeStart start = builder.startEditableRange();
builder.write("[type here]");
builder.endEditableRange();
builder.writeln("");
builder.writeln("This sentence is locked too.");
EditableRange range = start.getEditableRange();
System.out.println("Editable range id: " + range.getId());
System.out.println("Text of the paragraph that holds the range: " + range.getEditableRangeStart().getParentNode().getText().trim());
doc.protect(ProtectionType.READ_ONLY, "secret");
doc.save("form.docx");
Document reloaded = new Document("form.docx");
System.out.println("Protection after reload: " + ProtectionType.getName(reloaded.getProtectionType()));
NodeCollection starts = reloaded.getChildNodes(NodeType.EDITABLE_RANGE_START, true);
NodeCollection ends = reloaded.getChildNodes(NodeType.EDITABLE_RANGE_END, true);
System.out.println("Editable range starts: " + starts.getCount() + ", ends: " + ends.getCount());The program printed:
Editable range id: 0
Text of the paragraph that holds the range: Fill in your name: [type here]
Protection after reload: READ_ONLY
Editable range starts: 1, ends: 1Step 4: Read the Ranges Back
After saving and reloading, the protection type was READ_ONLY and getChildNodes(NodeType.EDITABLE_RANGE_START, true) found 1 range, with 1 matching end.
Step 5: Remove a Range
start.getEditableRange().remove() removes a range. The run went from 2 ranges to 1 and the text still read first area and second area.
import com.aspose.words.*;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
EditableRangeStart first = builder.startEditableRange();
builder.write("first area");
builder.endEditableRange();
builder.write(" and ");
EditableRangeStart second = builder.startEditableRange();
builder.write("second area");
builder.endEditableRange();
builder.writeln("");
System.out.println("Ranges before: " + doc.getChildNodes(NodeType.EDITABLE_RANGE_START, true).getCount());
second.getEditableRange().remove();
System.out.println("Ranges after remove: " + doc.getChildNodes(NodeType.EDITABLE_RANGE_START, true).getCount());
System.out.println("Text is unchanged: " + doc.getText().trim());The program printed:
Ranges before: 2
Ranges after remove: 1
Text is unchanged: first area and second areaCommon Issues and Fixes
I cannot tell if Word lets the reader edit the range. The examples read the saved file and do not run Word. Open the file in Word to check.
remove() did not remove the text. It removes the range markers only.
The compiler reports an unreported exception. The Document constructors, Document.save and some other methods declare throws Exception. Declare it on the calling method or catch it.
Frequently Asked Questions
Is the range limited to one user?
A new range had an empty single user in the test. The reference page describes editorGroup as an alias used to determine whether the current user may edit the range; the examples use neither property, so they do not show how Word applies them.
Which protection type did the example use?
ProtectionType.READ_ONLY with a password.