EditableRange
Inheritance: java.lang.Object
public class EditableRange
Represents a single editable range.
To learn more, visit the Aspose.Words Document Object Model (DOM) documentation article.
Remarks:
EditableRange is a “facade” object that encapsulates two nodes getEditableRangeStart() and getEditableRangeEnd() in a document tree and allows to work with an editable range as a single object.
Examples:
Shows how to work with an editable range.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// Editable ranges allow us to leave parts of protected documents open for editing.
EditableRangeStart editableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph is inside an editable range, and can be edited.");
EditableRangeEnd editableRangeEnd = builder.endEditableRange();
// A well-formed editable range has a start node, and end node.
// These nodes have matching IDs and encompass editable nodes.
EditableRange editableRange = editableRangeStart.getEditableRange();
Assert.assertEquals(editableRangeStart.getId(), editableRange.getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getId());
// Different parts of the editable range link to each other.
Assert.assertEquals(editableRangeStart.getId(), editableRange.getEditableRangeStart().getId());
Assert.assertEquals(editableRangeStart.getId(), editableRangeEnd.getEditableRangeStart().getId());
Assert.assertEquals(editableRange.getId(), editableRangeStart.getEditableRange().getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getEditableRangeEnd().getId());
// We can access the node types of each part like this. The editable range itself is not a node,
// but an entity which consists of a start, an end, and their enclosed contents.
Assert.assertEquals(NodeType.EDITABLE_RANGE_START, editableRangeStart.getNodeType());
Assert.assertEquals(NodeType.EDITABLE_RANGE_END, editableRangeEnd.getNodeType());
builder.writeln("This paragraph is outside the editable range, and cannot be edited.");
doc.save(getArtifactsDir() + "EditableRange.CreateAndRemove.docx");
// Remove an editable range. All the nodes that were inside the range will remain intact.
editableRange.remove();
Shows how to limit the editing rights of editable ranges to a specific group/user.
public void visitor() throws Exception {
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// When we write-protect documents, editable ranges allow us to pick specific areas that users may edit.
// There are two mutually exclusive ways to narrow down the list of allowed editors.
// 1 - Specify a user:
EditableRange editableRange = builder.startEditableRange().getEditableRange();
editableRange.setSingleUser("john.doe@myoffice.com");
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getSingleUser()));
builder.endEditableRange();
Assert.assertEquals(EditorType.UNSPECIFIED, editableRange.getEditorGroup());
// 2 - Specify a group that allowed users are associated with:
editableRange = builder.startEditableRange().getEditableRange();
editableRange.setEditorGroup(EditorType.ADMINISTRATORS);
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getEditorGroup()));
builder.endEditableRange();
Assert.assertEquals("", editableRange.getSingleUser());
builder.writeln("This paragraph is outside the editable range, and cannot be edited by anybody.");
// Print details and contents of every editable range in the document.
EditableRangePrinter editableRangePrinter = new EditableRangePrinter();
doc.accept(editableRangePrinter);
System.out.println(editableRangePrinter.toText());
}
///
/// Collects properties and contents of visited editable ranges in a string.
///
public static class EditableRangePrinter extends DocumentVisitor {
public EditableRangePrinter() {
mBuilder = new StringBuilder();
}
public String toText() {
return mBuilder.toString();
}
public void reset() {
mBuilder.setLength(0);
mInsideEditableRange = false;
}
///
/// Called when an EditableRangeStart node is encountered in the document.
///
public int visitEditableRangeStart(EditableRangeStart editableRangeStart) {
mBuilder.append(" -- Editable range found! -- ");
mBuilder.append("\tID:\t\t" + editableRangeStart.getId());
if (editableRangeStart.getEditableRange().getSingleUser().equals(""))
mBuilder.append("\tGroup:\t" + editableRangeStart.getEditableRange().getEditorGroup());
else
mBuilder.append("\tUser:\t" + editableRangeStart.getEditableRange().getSingleUser());
mBuilder.append("\tContents:");
mInsideEditableRange = true;
return VisitorAction.CONTINUE;
}
///
/// Called when an EditableRangeEnd node is encountered in the document.
///
public int visitEditableRangeEnd(final EditableRangeEnd editableRangeEnd) {
mBuilder.append(" -- End of editable range -- " + "\r\n");
mInsideEditableRange = false;
return VisitorAction.CONTINUE;
}
///
/// Called when a Run node is encountered in the document. This visitor only records runs that are inside editable ranges.
///
public int visitRun(final Run run) {
if (mInsideEditableRange) {
mBuilder.append("\t\"" + run.getText() + "\"" + "\r\n");
}
return VisitorAction.CONTINUE;
}
private boolean mInsideEditableRange;
private final StringBuilder mBuilder;
}
Methods
Method | Description |
---|---|
getEditableRangeEnd() | Gets the node that represents the end of the editable range. |
getEditableRangeStart() | Gets the node that represents the start of the editable range. |
getEditorGroup() | Gets an alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range. |
getId() | Gets the editable range identifier. |
getSingleUser() | Gets the single user for editable range. |
remove() | Removes the editable range from the document. |
setEditorGroup(int value) | Sets an alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range. |
setSingleUser(String value) | Sets the single user for editable range. |
getEditableRangeEnd()
public EditableRangeEnd getEditableRangeEnd()
Gets the node that represents the end of the editable range.
Examples:
Shows how to work with an editable range.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// Editable ranges allow us to leave parts of protected documents open for editing.
EditableRangeStart editableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph is inside an editable range, and can be edited.");
EditableRangeEnd editableRangeEnd = builder.endEditableRange();
// A well-formed editable range has a start node, and end node.
// These nodes have matching IDs and encompass editable nodes.
EditableRange editableRange = editableRangeStart.getEditableRange();
Assert.assertEquals(editableRangeStart.getId(), editableRange.getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getId());
// Different parts of the editable range link to each other.
Assert.assertEquals(editableRangeStart.getId(), editableRange.getEditableRangeStart().getId());
Assert.assertEquals(editableRangeStart.getId(), editableRangeEnd.getEditableRangeStart().getId());
Assert.assertEquals(editableRange.getId(), editableRangeStart.getEditableRange().getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getEditableRangeEnd().getId());
// We can access the node types of each part like this. The editable range itself is not a node,
// but an entity which consists of a start, an end, and their enclosed contents.
Assert.assertEquals(NodeType.EDITABLE_RANGE_START, editableRangeStart.getNodeType());
Assert.assertEquals(NodeType.EDITABLE_RANGE_END, editableRangeEnd.getNodeType());
builder.writeln("This paragraph is outside the editable range, and cannot be edited.");
doc.save(getArtifactsDir() + "EditableRange.CreateAndRemove.docx");
// Remove an editable range. All the nodes that were inside the range will remain intact.
editableRange.remove();
Returns: EditableRangeEnd - The node that represents the end of the editable range.
getEditableRangeStart()
public EditableRangeStart getEditableRangeStart()
Gets the node that represents the start of the editable range.
Examples:
Shows how to work with an editable range.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// Editable ranges allow us to leave parts of protected documents open for editing.
EditableRangeStart editableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph is inside an editable range, and can be edited.");
EditableRangeEnd editableRangeEnd = builder.endEditableRange();
// A well-formed editable range has a start node, and end node.
// These nodes have matching IDs and encompass editable nodes.
EditableRange editableRange = editableRangeStart.getEditableRange();
Assert.assertEquals(editableRangeStart.getId(), editableRange.getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getId());
// Different parts of the editable range link to each other.
Assert.assertEquals(editableRangeStart.getId(), editableRange.getEditableRangeStart().getId());
Assert.assertEquals(editableRangeStart.getId(), editableRangeEnd.getEditableRangeStart().getId());
Assert.assertEquals(editableRange.getId(), editableRangeStart.getEditableRange().getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getEditableRangeEnd().getId());
// We can access the node types of each part like this. The editable range itself is not a node,
// but an entity which consists of a start, an end, and their enclosed contents.
Assert.assertEquals(NodeType.EDITABLE_RANGE_START, editableRangeStart.getNodeType());
Assert.assertEquals(NodeType.EDITABLE_RANGE_END, editableRangeEnd.getNodeType());
builder.writeln("This paragraph is outside the editable range, and cannot be edited.");
doc.save(getArtifactsDir() + "EditableRange.CreateAndRemove.docx");
// Remove an editable range. All the nodes that were inside the range will remain intact.
editableRange.remove();
Returns: EditableRangeStart - The node that represents the start of the editable range.
getEditorGroup()
public int getEditorGroup()
Gets an alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range.
Remarks:
Single user and editor group cannot be set simultaneously for the specific editable range, if the one is set, the other will be clear.
Examples:
Shows how to create nested editable ranges.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only, " +
"we cannot edit this paragraph without the password.");
// Create two nested editable ranges.
EditableRangeStart outerEditableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph inside the outer editable range and can be edited.");
EditableRangeStart innerEditableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph inside both the outer and inner editable ranges and can be edited.");
// Currently, the document builder's node insertion cursor is in more than one ongoing editable range.
// When we want to end an editable range in this situation,
// we need to specify which of the ranges we wish to end by passing its EditableRangeStart node.
builder.endEditableRange(innerEditableRangeStart);
builder.writeln("This paragraph inside the outer editable range and can be edited.");
builder.endEditableRange(outerEditableRangeStart);
builder.writeln("This paragraph is outside any editable ranges, and cannot be edited.");
// If a region of text has two overlapping editable ranges with specified groups,
// the combined group of users excluded by both groups are prevented from editing it.
outerEditableRangeStart.getEditableRange().setEditorGroup(EditorType.EVERYONE);
innerEditableRangeStart.getEditableRange().setEditorGroup(EditorType.CONTRIBUTORS);
doc.save(getArtifactsDir() + "EditableRange.Nested.docx");
Shows how to limit the editing rights of editable ranges to a specific group/user.
public void visitor() throws Exception {
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// When we write-protect documents, editable ranges allow us to pick specific areas that users may edit.
// There are two mutually exclusive ways to narrow down the list of allowed editors.
// 1 - Specify a user:
EditableRange editableRange = builder.startEditableRange().getEditableRange();
editableRange.setSingleUser("john.doe@myoffice.com");
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getSingleUser()));
builder.endEditableRange();
Assert.assertEquals(EditorType.UNSPECIFIED, editableRange.getEditorGroup());
// 2 - Specify a group that allowed users are associated with:
editableRange = builder.startEditableRange().getEditableRange();
editableRange.setEditorGroup(EditorType.ADMINISTRATORS);
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getEditorGroup()));
builder.endEditableRange();
Assert.assertEquals("", editableRange.getSingleUser());
builder.writeln("This paragraph is outside the editable range, and cannot be edited by anybody.");
// Print details and contents of every editable range in the document.
EditableRangePrinter editableRangePrinter = new EditableRangePrinter();
doc.accept(editableRangePrinter);
System.out.println(editableRangePrinter.toText());
}
///
/// Collects properties and contents of visited editable ranges in a string.
///
public static class EditableRangePrinter extends DocumentVisitor {
public EditableRangePrinter() {
mBuilder = new StringBuilder();
}
public String toText() {
return mBuilder.toString();
}
public void reset() {
mBuilder.setLength(0);
mInsideEditableRange = false;
}
///
/// Called when an EditableRangeStart node is encountered in the document.
///
public int visitEditableRangeStart(EditableRangeStart editableRangeStart) {
mBuilder.append(" -- Editable range found! -- ");
mBuilder.append("\tID:\t\t" + editableRangeStart.getId());
if (editableRangeStart.getEditableRange().getSingleUser().equals(""))
mBuilder.append("\tGroup:\t" + editableRangeStart.getEditableRange().getEditorGroup());
else
mBuilder.append("\tUser:\t" + editableRangeStart.getEditableRange().getSingleUser());
mBuilder.append("\tContents:");
mInsideEditableRange = true;
return VisitorAction.CONTINUE;
}
///
/// Called when an EditableRangeEnd node is encountered in the document.
///
public int visitEditableRangeEnd(final EditableRangeEnd editableRangeEnd) {
mBuilder.append(" -- End of editable range -- " + "\r\n");
mInsideEditableRange = false;
return VisitorAction.CONTINUE;
}
///
/// Called when a Run node is encountered in the document. This visitor only records runs that are inside editable ranges.
///
public int visitRun(final Run run) {
if (mInsideEditableRange) {
mBuilder.append("\t\"" + run.getText() + "\"" + "\r\n");
}
return VisitorAction.CONTINUE;
}
private boolean mInsideEditableRange;
private final StringBuilder mBuilder;
}
Returns: int - An alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range. The returned value is one of EditorType constants.
getId()
public int getId()
Gets the editable range identifier.
Remarks:
The region must be demarcated using the getEditableRangeStart() and getEditableRangeEnd()
Editable range identifiers are supposed to be unique across a document and Aspose.Words automatically maintains editable range identifiers when loading, saving and combining documents.
Examples:
Shows how to work with an editable range.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// Editable ranges allow us to leave parts of protected documents open for editing.
EditableRangeStart editableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph is inside an editable range, and can be edited.");
EditableRangeEnd editableRangeEnd = builder.endEditableRange();
// A well-formed editable range has a start node, and end node.
// These nodes have matching IDs and encompass editable nodes.
EditableRange editableRange = editableRangeStart.getEditableRange();
Assert.assertEquals(editableRangeStart.getId(), editableRange.getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getId());
// Different parts of the editable range link to each other.
Assert.assertEquals(editableRangeStart.getId(), editableRange.getEditableRangeStart().getId());
Assert.assertEquals(editableRangeStart.getId(), editableRangeEnd.getEditableRangeStart().getId());
Assert.assertEquals(editableRange.getId(), editableRangeStart.getEditableRange().getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getEditableRangeEnd().getId());
// We can access the node types of each part like this. The editable range itself is not a node,
// but an entity which consists of a start, an end, and their enclosed contents.
Assert.assertEquals(NodeType.EDITABLE_RANGE_START, editableRangeStart.getNodeType());
Assert.assertEquals(NodeType.EDITABLE_RANGE_END, editableRangeEnd.getNodeType());
builder.writeln("This paragraph is outside the editable range, and cannot be edited.");
doc.save(getArtifactsDir() + "EditableRange.CreateAndRemove.docx");
// Remove an editable range. All the nodes that were inside the range will remain intact.
editableRange.remove();
Returns: int - The editable range identifier.
getSingleUser()
public String getSingleUser()
Gets the single user for editable range.
Remarks:
This editor can be stored in one of the following forms:
DOMAIN\Username - for users whose access shall be authenticated using the current user’s domain credentials.
user@domain.com - for users whose access shall be authenticated using the user’s e-mail address as credentials.
user - for users whose access shall be authenticated using the current user’s machine credentials.
Single user and editor group cannot be set simultaneously for the specific editable range, if the one is set, the other will be clear.
Examples:
Shows how to limit the editing rights of editable ranges to a specific group/user.
public void visitor() throws Exception {
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// When we write-protect documents, editable ranges allow us to pick specific areas that users may edit.
// There are two mutually exclusive ways to narrow down the list of allowed editors.
// 1 - Specify a user:
EditableRange editableRange = builder.startEditableRange().getEditableRange();
editableRange.setSingleUser("john.doe@myoffice.com");
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getSingleUser()));
builder.endEditableRange();
Assert.assertEquals(EditorType.UNSPECIFIED, editableRange.getEditorGroup());
// 2 - Specify a group that allowed users are associated with:
editableRange = builder.startEditableRange().getEditableRange();
editableRange.setEditorGroup(EditorType.ADMINISTRATORS);
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getEditorGroup()));
builder.endEditableRange();
Assert.assertEquals("", editableRange.getSingleUser());
builder.writeln("This paragraph is outside the editable range, and cannot be edited by anybody.");
// Print details and contents of every editable range in the document.
EditableRangePrinter editableRangePrinter = new EditableRangePrinter();
doc.accept(editableRangePrinter);
System.out.println(editableRangePrinter.toText());
}
///
/// Collects properties and contents of visited editable ranges in a string.
///
public static class EditableRangePrinter extends DocumentVisitor {
public EditableRangePrinter() {
mBuilder = new StringBuilder();
}
public String toText() {
return mBuilder.toString();
}
public void reset() {
mBuilder.setLength(0);
mInsideEditableRange = false;
}
///
/// Called when an EditableRangeStart node is encountered in the document.
///
public int visitEditableRangeStart(EditableRangeStart editableRangeStart) {
mBuilder.append(" -- Editable range found! -- ");
mBuilder.append("\tID:\t\t" + editableRangeStart.getId());
if (editableRangeStart.getEditableRange().getSingleUser().equals(""))
mBuilder.append("\tGroup:\t" + editableRangeStart.getEditableRange().getEditorGroup());
else
mBuilder.append("\tUser:\t" + editableRangeStart.getEditableRange().getSingleUser());
mBuilder.append("\tContents:");
mInsideEditableRange = true;
return VisitorAction.CONTINUE;
}
///
/// Called when an EditableRangeEnd node is encountered in the document.
///
public int visitEditableRangeEnd(final EditableRangeEnd editableRangeEnd) {
mBuilder.append(" -- End of editable range -- " + "\r\n");
mInsideEditableRange = false;
return VisitorAction.CONTINUE;
}
///
/// Called when a Run node is encountered in the document. This visitor only records runs that are inside editable ranges.
///
public int visitRun(final Run run) {
if (mInsideEditableRange) {
mBuilder.append("\t\"" + run.getText() + "\"" + "\r\n");
}
return VisitorAction.CONTINUE;
}
private boolean mInsideEditableRange;
private final StringBuilder mBuilder;
}
Returns: java.lang.String - The single user for editable range.
remove()
public void remove()
Removes the editable range from the document. Does not remove content inside the editable range.
Examples:
Shows how to work with an editable range.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// Editable ranges allow us to leave parts of protected documents open for editing.
EditableRangeStart editableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph is inside an editable range, and can be edited.");
EditableRangeEnd editableRangeEnd = builder.endEditableRange();
// A well-formed editable range has a start node, and end node.
// These nodes have matching IDs and encompass editable nodes.
EditableRange editableRange = editableRangeStart.getEditableRange();
Assert.assertEquals(editableRangeStart.getId(), editableRange.getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getId());
// Different parts of the editable range link to each other.
Assert.assertEquals(editableRangeStart.getId(), editableRange.getEditableRangeStart().getId());
Assert.assertEquals(editableRangeStart.getId(), editableRangeEnd.getEditableRangeStart().getId());
Assert.assertEquals(editableRange.getId(), editableRangeStart.getEditableRange().getId());
Assert.assertEquals(editableRangeEnd.getId(), editableRange.getEditableRangeEnd().getId());
// We can access the node types of each part like this. The editable range itself is not a node,
// but an entity which consists of a start, an end, and their enclosed contents.
Assert.assertEquals(NodeType.EDITABLE_RANGE_START, editableRangeStart.getNodeType());
Assert.assertEquals(NodeType.EDITABLE_RANGE_END, editableRangeEnd.getNodeType());
builder.writeln("This paragraph is outside the editable range, and cannot be edited.");
doc.save(getArtifactsDir() + "EditableRange.CreateAndRemove.docx");
// Remove an editable range. All the nodes that were inside the range will remain intact.
editableRange.remove();
setEditorGroup(int value)
public void setEditorGroup(int value)
Sets an alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range.
Remarks:
Single user and editor group cannot be set simultaneously for the specific editable range, if the one is set, the other will be clear.
Examples:
Shows how to create nested editable ranges.
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only, " +
"we cannot edit this paragraph without the password.");
// Create two nested editable ranges.
EditableRangeStart outerEditableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph inside the outer editable range and can be edited.");
EditableRangeStart innerEditableRangeStart = builder.startEditableRange();
builder.writeln("This paragraph inside both the outer and inner editable ranges and can be edited.");
// Currently, the document builder's node insertion cursor is in more than one ongoing editable range.
// When we want to end an editable range in this situation,
// we need to specify which of the ranges we wish to end by passing its EditableRangeStart node.
builder.endEditableRange(innerEditableRangeStart);
builder.writeln("This paragraph inside the outer editable range and can be edited.");
builder.endEditableRange(outerEditableRangeStart);
builder.writeln("This paragraph is outside any editable ranges, and cannot be edited.");
// If a region of text has two overlapping editable ranges with specified groups,
// the combined group of users excluded by both groups are prevented from editing it.
outerEditableRangeStart.getEditableRange().setEditorGroup(EditorType.EVERYONE);
innerEditableRangeStart.getEditableRange().setEditorGroup(EditorType.CONTRIBUTORS);
doc.save(getArtifactsDir() + "EditableRange.Nested.docx");
Shows how to limit the editing rights of editable ranges to a specific group/user.
public void visitor() throws Exception {
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// When we write-protect documents, editable ranges allow us to pick specific areas that users may edit.
// There are two mutually exclusive ways to narrow down the list of allowed editors.
// 1 - Specify a user:
EditableRange editableRange = builder.startEditableRange().getEditableRange();
editableRange.setSingleUser("john.doe@myoffice.com");
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getSingleUser()));
builder.endEditableRange();
Assert.assertEquals(EditorType.UNSPECIFIED, editableRange.getEditorGroup());
// 2 - Specify a group that allowed users are associated with:
editableRange = builder.startEditableRange().getEditableRange();
editableRange.setEditorGroup(EditorType.ADMINISTRATORS);
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getEditorGroup()));
builder.endEditableRange();
Assert.assertEquals("", editableRange.getSingleUser());
builder.writeln("This paragraph is outside the editable range, and cannot be edited by anybody.");
// Print details and contents of every editable range in the document.
EditableRangePrinter editableRangePrinter = new EditableRangePrinter();
doc.accept(editableRangePrinter);
System.out.println(editableRangePrinter.toText());
}
///
/// Collects properties and contents of visited editable ranges in a string.
///
public static class EditableRangePrinter extends DocumentVisitor {
public EditableRangePrinter() {
mBuilder = new StringBuilder();
}
public String toText() {
return mBuilder.toString();
}
public void reset() {
mBuilder.setLength(0);
mInsideEditableRange = false;
}
///
/// Called when an EditableRangeStart node is encountered in the document.
///
public int visitEditableRangeStart(EditableRangeStart editableRangeStart) {
mBuilder.append(" -- Editable range found! -- ");
mBuilder.append("\tID:\t\t" + editableRangeStart.getId());
if (editableRangeStart.getEditableRange().getSingleUser().equals(""))
mBuilder.append("\tGroup:\t" + editableRangeStart.getEditableRange().getEditorGroup());
else
mBuilder.append("\tUser:\t" + editableRangeStart.getEditableRange().getSingleUser());
mBuilder.append("\tContents:");
mInsideEditableRange = true;
return VisitorAction.CONTINUE;
}
///
/// Called when an EditableRangeEnd node is encountered in the document.
///
public int visitEditableRangeEnd(final EditableRangeEnd editableRangeEnd) {
mBuilder.append(" -- End of editable range -- " + "\r\n");
mInsideEditableRange = false;
return VisitorAction.CONTINUE;
}
///
/// Called when a Run node is encountered in the document. This visitor only records runs that are inside editable ranges.
///
public int visitRun(final Run run) {
if (mInsideEditableRange) {
mBuilder.append("\t\"" + run.getText() + "\"" + "\r\n");
}
return VisitorAction.CONTINUE;
}
private boolean mInsideEditableRange;
private final StringBuilder mBuilder;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | An alias (or editing group) which shall be used to determine if the current user shall be allowed to edit this editable range. The value must be one of EditorType constants. |
setSingleUser(String value)
public void setSingleUser(String value)
Sets the single user for editable range.
Remarks:
This editor can be stored in one of the following forms:
DOMAIN\Username - for users whose access shall be authenticated using the current user’s domain credentials.
user@domain.com - for users whose access shall be authenticated using the user’s e-mail address as credentials.
user - for users whose access shall be authenticated using the current user’s machine credentials.
Single user and editor group cannot be set simultaneously for the specific editable range, if the one is set, the other will be clear.
Examples:
Shows how to limit the editing rights of editable ranges to a specific group/user.
public void visitor() throws Exception {
Document doc = new Document();
doc.protect(ProtectionType.READ_ONLY, "MyPassword");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Hello world! Since we have set the document's protection level to read-only," +
" we cannot edit this paragraph without the password.");
// When we write-protect documents, editable ranges allow us to pick specific areas that users may edit.
// There are two mutually exclusive ways to narrow down the list of allowed editors.
// 1 - Specify a user:
EditableRange editableRange = builder.startEditableRange().getEditableRange();
editableRange.setSingleUser("john.doe@myoffice.com");
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getSingleUser()));
builder.endEditableRange();
Assert.assertEquals(EditorType.UNSPECIFIED, editableRange.getEditorGroup());
// 2 - Specify a group that allowed users are associated with:
editableRange = builder.startEditableRange().getEditableRange();
editableRange.setEditorGroup(EditorType.ADMINISTRATORS);
builder.writeln(MessageFormat.format("This paragraph is inside the first editable range, can only be edited by {0}.", editableRange.getEditorGroup()));
builder.endEditableRange();
Assert.assertEquals("", editableRange.getSingleUser());
builder.writeln("This paragraph is outside the editable range, and cannot be edited by anybody.");
// Print details and contents of every editable range in the document.
EditableRangePrinter editableRangePrinter = new EditableRangePrinter();
doc.accept(editableRangePrinter);
System.out.println(editableRangePrinter.toText());
}
///
/// Collects properties and contents of visited editable ranges in a string.
///
public static class EditableRangePrinter extends DocumentVisitor {
public EditableRangePrinter() {
mBuilder = new StringBuilder();
}
public String toText() {
return mBuilder.toString();
}
public void reset() {
mBuilder.setLength(0);
mInsideEditableRange = false;
}
///
/// Called when an EditableRangeStart node is encountered in the document.
///
public int visitEditableRangeStart(EditableRangeStart editableRangeStart) {
mBuilder.append(" -- Editable range found! -- ");
mBuilder.append("\tID:\t\t" + editableRangeStart.getId());
if (editableRangeStart.getEditableRange().getSingleUser().equals(""))
mBuilder.append("\tGroup:\t" + editableRangeStart.getEditableRange().getEditorGroup());
else
mBuilder.append("\tUser:\t" + editableRangeStart.getEditableRange().getSingleUser());
mBuilder.append("\tContents:");
mInsideEditableRange = true;
return VisitorAction.CONTINUE;
}
///
/// Called when an EditableRangeEnd node is encountered in the document.
///
public int visitEditableRangeEnd(final EditableRangeEnd editableRangeEnd) {
mBuilder.append(" -- End of editable range -- " + "\r\n");
mInsideEditableRange = false;
return VisitorAction.CONTINUE;
}
///
/// Called when a Run node is encountered in the document. This visitor only records runs that are inside editable ranges.
///
public int visitRun(final Run run) {
if (mInsideEditableRange) {
mBuilder.append("\t\"" + run.getText() + "\"" + "\r\n");
}
return VisitorAction.CONTINUE;
}
private boolean mInsideEditableRange;
private final StringBuilder mBuilder;
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The single user for editable range. |