Back to Devexpress

How to: Grant Editing Permissions in Code

windowsforms-116603-controls-and-libraries-rich-text-editor-examples-protection-how-to-grant-editing-permissions-in-code.md

latest5.2 KB
Original Source

How to: Grant Editing Permissions in Code

  • Feb 24, 2025
  • 2 minutes to read

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.

  5. Use the Document.Protect method to protect the document from modification. With document protection enabled, ranges without edit permission are read-only.

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Services;

// Create document ranges.
DocumentRange rangeAdmin = AppendDocument("Documents\\administrator.docx");
DocumentRange rangeBody = AppendDocument("Documents\\body.docx");
DocumentRange rangeSignature = AppendDocument("Documents\\signature.docx");

// Protect document ranges.
RangePermissionCollection rangePermissions = richEditControl1.Document.BeginUpdateRangePermissions();
rangePermissions.AddRange(CreateRangePermissions(rangeAdmin, String.Empty, "[email protected]", "Nancy Skywalker"));
rangePermissions.AddRange(CreateRangePermissions(rangeBody, "Everyone", String.Empty));
rangePermissions.AddRange(CreateRangePermissions(rangeSignature, "Skywalkers", String.Empty));            
richEditControl1.Document.EndUpdateRangePermissions(rangePermissions);
// Enforce protection and set password.
richEditControl1.Document.Protect("123");

private DocumentRange AppendDocument(string filename)
{
    richEditControl1.Document.Paragraphs.Insert(richEditControl1.Document.Range.End);
    DocumentPosition pos = richEditControl1.Document.CreatePosition(richEditControl1.Document.Range.End.ToInt() - 2);
    DocumentRange range = richEditControl1.Document.InsertDocumentContent(pos, filename, DocumentFormat.Docx);
    return range;
}

private static List<RangePermission> CreateRangePermissions(DocumentRange range, string userGroup, params string[] usernames)
{
    List<RangePermission> rangeList = new List<RangePermission>();
    foreach (string username in usernames)
    {
        RangePermission rp = new RangePermission(range);
        rp.Group = userGroup;
        rp.UserName = username;
        rangeList.Add(rp);
    }
    return rangeList;
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit.Services

' Create document ranges.
Dim rangeAdmin As DocumentRange = AppendDocument("Documents\administrator.docx")
Dim rangeBody As DocumentRange = AppendDocument("Documents\body.docx")
Dim rangeSignature As DocumentRange = AppendDocument("Documents\signature.docx")

' Protect document ranges.
Dim rangePermissions As RangePermissionCollection = richEditControl1.Document.BeginUpdateRangePermissions()
rangePermissions.AddRange(CreateRangePermissions(rangeAdmin, String.Empty, "[email protected]", "Nancy Skywalker"))
rangePermissions.AddRange(CreateRangePermissions(rangeBody, "Everyone", String.Empty))
rangePermissions.AddRange(CreateRangePermissions(rangeSignature, "Skywalkers", String.Empty))
richEditControl1.Document.EndUpdateRangePermissions(rangePermissions)
' Enforce protection and set password.
richEditControl1.Document.Protect("123")

Private Function AppendDocument(ByVal filename As String) As DocumentRange
    richEditControl1.Document.Paragraphs.Insert(richEditControl1.Document.Range.End)
    Dim pos As DocumentPosition = richEditControl1.Document.CreatePosition(richEditControl1.Document.Range.End.ToInt() - 2)
    Dim range As DocumentRange = richEditControl1.Document.InsertDocumentContent(pos, filename, DocumentFormat.Docx)
    Return range
End Function

Private Shared Function CreateRangePermissions(ByVal range As DocumentRange, ByVal userGroup As String, ParamArray ByVal usernames() As String) As List(Of RangePermission)
    Dim rangeList As New List(Of RangePermission)()
    For Each username As String In usernames
        Dim rp As New RangePermission(range)
        rp.Group = userGroup
        rp.UserName = username
        rangeList.Add(rp)
    Next username
    Return rangeList
End Function