Skip to main content
A newer version of this page is available. .

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:

  1. Access the document collection of ranges with permissions by calling the SubDocument.BeginUpdateRangePermissions method.
  2. Call the RangePermissionCollection.CreateRangePermission method to create a RangePermission instance to the target document range.
  3. Specify the user and/or the group of users that are eligible to edit the document using the RangePermission.Group and RangePermission.UserName properties.
  4. 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");