Manage Range Permissions
- 5 minutes to read
Range permissions allow specific users or user groups to edit document ranges in a protected document. Other document content remains read-only for these users.
The Word Processing Document API stores range permissions in the WordDocument.RangePermissions collection. The collection contains range permissions from the main document body, headers, footers, comments, and text boxes.
Allow Users to Edit a Document Range
Follow the steps below to allow users to edit a document range:
- Get the range of a document element (for example, a paragraph). Call the DocumentFragment.GetRange(element) method, or create a DocumentRange object with a start position and length.
- Create a RangePermissionDefinition object. The
RangePermissionDefinition(userName, group)constructor specifies the user or user group that can edit the range. - Pass the range and the
RangePermissionDefinitionobject to the RangePermissionCollection.Create(range, rangePermissionDefinition) method. - Call the WordDocument.Protect(options) method to restrict editing in the document. Set the DocumentProtectionOptions.ProtectionType property to
ReadOnlyorAllowComments.
Specify one of the following values in the RangePermissionDefinition(userName, group) constructor:
| Users That Can Edit the Range | userName |
group |
|---|---|---|
| A specific user | The user name (for example, an email address or a user name in the DOMAIN\user format). | null |
| All users | null |
Everyone |
The following code snippet allows all users to edit the first paragraph, allows a specific user to edit the second paragraph, and restricts editing in other document content:
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Word;
Directory.CreateDirectory("output");
using var inputStream = File.OpenRead("Document.docx");
using var document = new WordDocument(inputStream);
var body = document.Body;
var firstParagraph = (Paragraph)body[0];
document.RangePermissions.Create(body.GetRange(firstParagraph), new RangePermissionDefinition(null, "Everyone"));
var secondParagraph = (Paragraph)body[1];
document.RangePermissions.Create(body.GetRange(secondParagraph), new RangePermissionDefinition("john.smith@example.com", null));
var protectionOptions = new DocumentProtectionOptions("password", HashAlgorithmType.Sha512) {
ProtectionType = DocumentProtectionType.ReadOnly
};
document.Protect(protectionOptions);
using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
document.Save(outputStream, DocumentFormat.Docx);
Note
Protection settings apply to users in word processing applications (for example, Microsoft Word). The Word Processing Document API does not check protection settings when your code modifies a protected document.
Adjacent and Overlapping Ranges
If the specified range overlaps or adjoins a range permission with the same user name and group, the Create method merges the ranges into a single range permission. The Create method returns the merged range permission. The API removes the previous RangePermission object from the collection, and the MarkedRangeBase.Invalidated property of the previous object gets true.
Get and Modify Range Permissions
Iterate the WordDocument.RangePermissions collection or use the RangePermissionCollection[index] indexer to access range permissions. Use the following properties to get and modify range permission settings:
| Property | Description |
|---|---|
| RangePermission.UserName | Gets or sets the name of the user who can edit the range in a protected document. |
| RangePermission.Group | Gets or sets the name of the user group that can edit the range in a protected document. |
| MarkedRangeBase.Range | Gets or sets the document range of the bookmark, comment, or range permission. |
| MarkedRangeBase.DocumentFragment | Gets the document fragment that contains the bookmark, comment, or range permission. |
To get the text in restricted ranges, pass the Range property value to the DocumentFragment.GetText(range) method of the document fragment that contains the range permission.
Remove Range Permissions
Call the RangePermissionCollection.Remove(rangePermission) method to remove a range permission. The Remove method does not modify document content.
If you delete all content in a range permission, the API removes the range permission from the collection.
After the API removes a range permission, the Invalidated property of the RangePermission object gets true. If you access UserName, Group, or Range properties of this object, the API throws an InvalidOperationException.
The following code snippet writes the user name, group, and text of each range permission to the console and removes range permissions for the Everyone group:
using System;
using System.IO;
using DevExpress.Docs.Word;
Directory.CreateDirectory("output");
using var inputStream = File.OpenRead("Document.docx");
using var document = new WordDocument(inputStream);
var rangePermissions = document.RangePermissions;
foreach (var rangePermission in rangePermissions) {
var text = rangePermission.DocumentFragment.GetText(rangePermission.Range).Trim();
Console.WriteLine($"User: {rangePermission.UserName}, group: {rangePermission.Group}, text: {text}");
}
for (int i = rangePermissions.Count - 1; i >= 0; i--) {
if (rangePermissions[i].Group == "Everyone")
rangePermissions.Remove(rangePermissions[i]);
}
using var outputStream = File.Create(Path.Combine("output", "Document.docx"));
document.Save(outputStream, DocumentFormat.Docx);
To remove all editing restrictions from a document, call the WordDocument.RemoveProtection() method. Range permissions remain in the document.