How to: Grant Editing Permissions in a Document
This code snippet illustrates how to unlock specific document ranges in a protected document for authenticated users by doing the following:
- Access the document collection of ranges with permissions by calling the SubDocument.BeginUpdateRangePermissions method.
- Call the RangePermissionCollection.CreateRangePermission method to create a RangePermission instance to the target document range.
- Specify the user and/or the group of users that are eligible to edit the document using the RangePermission.Group and RangePermission.UserName properties.
- Finish the update by calling the SubDocument.EndUpdateRangePermissions method.
server.LoadDocument("Documents//Grimm.docx", DocumentFormat.OpenXml);
Document document = server.Document;
// Protect document range
RangePermissionCollection rangePermissions = document.BeginUpdateRangePermissions();
RangePermission rp = rangePermissions.CreateRangePermission(document.Paragraphs[3].Range);
rp.Group = "Administrators";
rp.UserName = "admin@somecompany.com";
rangePermissions.Add(rp);
document.EndUpdateRangePermissions(rangePermissions);
// Enforce protection and set password.
document.Protect("123");