# Document Protection and Editable Regions

> Protect documents in a Windows Forms TextControl with EditMode and DocumentPermissions, mark editable regions per user and use the Permissions ribbon tab.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Editor > Document Protection and Editable Regions
- **4014 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/llms-full.txt

---

Forms, contracts and templates often have to stay fixed except for a few places that certain people fill in. A protected document in a `TextControl` is read-only or restricted as a whole, and *editable regions* are the exceptions: ranges of text that everyone, or only a named user, may still change. Protection is set up with the **Permissions** tab of the ribbon or in code, and it travels with the document file.

The code samples use *Form1.cs* with an editor named `textControl1`, the form of [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt). This page covers protection inside the editor; how far it protects a document against other programs is discussed in [Document Protection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/security/document-protection/llms-full.txt).

### How Document Protection Works

The [EditMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editmode-property/llms-full.txt) property of the `TextControl` decides whether a document is protected:

| Value | Effect |
|---|---|
| `Edit` | The text can be edited and formatted without restrictions |
| `ReadAndSelect` | The text can be read and selected; editing and formatting are restricted as the document permissions say |
| `ReadOnly` | The text is only displayed and can't be selected |
| `UsePassword` | Combined with `ReadAndSelect` by the bitwise OR operator, a dialog box asks for a password that protects the restrictions |

The restrictions of `ReadAndSelect` are the [DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/documentpermissions-property/llms-full.txt), a [DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/llms-full.txt) object. All of its properties are `true` by default:

| Property | Allows |
|---|---|
| [ReadOnly](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/readonly-property/llms-full.txt) | Nothing: when `true`, neither the text nor its formatting can be changed |
| [AllowFormatting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowformatting-property/llms-full.txt) | Formatting the document |
| [AllowFormattingStyles](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowformattingstyles-property/llms-full.txt) | Formatting with styles, which needs `AllowFormatting` as well |
| [AllowCopy](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowcopy-property/llms-full.txt) | Copying content to the clipboard |
| [AllowPrinting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowprinting-property/llms-full.txt) | Printing the document |
| [AllowEditingFormFields](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/alloweditingformfields-property/llms-full.txt) | Filling in form fields |

The permissions and the password are saved in the document in encrypted form, in RTF, DOC, DOCX, PDF and the TX Text Control format (TX). A document protected with a password opens read-only; to lift the protection, the user sets the edit mode back to `Edit` and enters the same password.

*Editable regions* stay editable in `ReadAndSelect` mode. Each region has a user name: only that user may edit it, or everyone if the name is empty. The users of the editor are listed in [UserNames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/usernames-property/llms-full.txt). Regions can overlap, and they are highlighted with a color that depends on the user.

### Protect a Document With the Ribbon

The **Permissions** tab, a [RibbonPermissionsTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonpermissionstab/llms-full.txt), holds all protection settings. Add it to the ribbon with the smart tag of the ribbon, as [Built-in Ribbon Tabs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/ribbon/llms-full.txt) describes. A user protects a document with it in four steps:

1. In the groups **Restrict Formatting** and **Restrict Editing**, choose what remains allowed, such as **Allow Printing**, **Allow Copy** and **Fill in Form Fields**, or click **Read Only**.
2. To add a user, click the dialog box launcher of the **Mark Exceptions and set Users** group, enter the name in the **Add Users** dialog box and click **Add**:

    ![The Permissions tab of TX Text Control Words with the Add Users dialog box open, which has a New User box, an Add button and a Users list containing John.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/permissions-add-users.0e0e98fb.webp)
3. Select the text the user may change and check the box of the user in the list of the group. **Every User** makes the selection editable for everybody:

    ![The Permissions tab with the user John checked in the Mark Exceptions and set Users group and the words general section selected and highlighted in the document.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/permissions-user-exception.f602a211.webp)
4. Click **Enforce Protection** and enter a password.

To remove the protection, the user clicks **Enforce Protection** again and enters the password:

![The Unprotect Document dialog box asking for the password over a document whose last paragraph, editable by all users, is highlighted in green.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/permissions-unprotect.951ba482.webp)

**Highlight Editable Regions** turns the highlighting on and off, and **Previous Editable Region** and **Next Editable region** move between the regions of the current user.

The names in the **Add Users** dialog box can come from your application. [RegisteredUserNames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonpermissionstab/registeredusernames-property/llms-full.txt) of the tab lists the names it offers, and [AllowAddingUserNames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonpermissionstab/allowaddingusernames-property/llms-full.txt) set to `false` prevents users from typing other names:

```csharp
// Offer only the users of the application in the Add Users dialog box
ribbonPermissionsTab1.RegisteredUserNames = new string[] { "Tim Typer", "Ryan Reviewer" };
ribbonPermissionsTab1.AllowAddingUserNames = false;
```

`ribbonPermissionsTab1` is the name the designer gave the tab.

### Mark Editable Regions in Code

[EditableRegions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregions-property/llms-full.txt) returns the [EditableRegionCollection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/llms-full.txt) of the document. Its [Add](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/add-method/llms-full.txt) method inserts an [EditableRegion](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregion/llms-full.txt):

```csharp
private void markSelectionButton_Click(object sender, EventArgs e)
{
    // Everyone may edit the selected text; the region has the ID 1
    EditableRegionCollection.AddResult result =
        textControl1.EditableRegions.Add(new EditableRegion(null, 1));

    if (result == EditableRegionCollection.AddResult.NoSelection)
    {
        MessageBox.Show("Select the text that should stay editable.");
    }
}

private void markForUserButton_Click(object sender, EventArgs e)
{
    // Only Tim Typer may edit characters 1 to 20; the start is one-based
    textControl1.EditableRegions.Add(new EditableRegion("Tim Typer", 2, 1, 20));
}
```

The code works as follows:

- The first parameter of the constructor is the user name. `null` or an empty string makes the region editable for everyone.
- The second parameter is an identifier of your choice, from 1 up; 0 means no identifier. [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/getitem-method/llms-full.txt) finds a region by it.
- Without a start and a length, the region covers the current selection. With them, it covers that range; the first character has the position 1.
- `Add` returns a value of [EditableRegionCollection.AddResult](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/addresult-enumeration/llms-full.txt), for example `Successful`, `NoSelection`, or `Combined` when the new region was merged with an overlapping or adjacent region of the same user and ID.

### Protect the Document in Code

After marking the regions, set the permissions and switch to `ReadAndSelect`. With `UsePassword`, the user is asked for the password:

```csharp
private void protectButton_Click(object sender, EventArgs e)
{
    // What remains allowed outside the editable regions
    textControl1.DocumentPermissions.ReadOnly = true;
    textControl1.DocumentPermissions.AllowPrinting = true;
    textControl1.DocumentPermissions.AllowCopy = false;

    // Protect the document; a dialog box asks for the password
    textControl1.EditMode = EditMode.ReadAndSelect | EditMode.UsePassword;

    textControl1.Save("contract.docx", StreamType.WordprocessingML);
}
```

Without `UsePassword`, the restrictions apply as well, but no password protects them: setting `EditMode` back to `Edit` lifts them. According to Text Control, the permissions are saved in the document only together with a password, so use `UsePassword` for documents that leave the editor.

### Open a Protected Document

Set [UserNames](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/usernames-property/llms-full.txt) to the signed-in user before the document is loaded. The regions of this user and the regions for everyone can then be edited:

```csharp
private void Form1_Load(object sender, EventArgs e)
{
    // The regions of these users can be edited
    textControl1.UserNames = new string[] { "Tim Typer" };

    textControl1.Load("contract.docx", StreamType.WordprocessingML);
}
```

TX Text Control compares the names and doesn't check who the user is, so take them from your application's sign-in, as [User Names Aren't Sign-Ins](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/security/document-protection/#user-names-aren-t-sign-ins) explains. The first name of the list is also the author of tracked changes and comments, as [Track Changes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/track-changes/llms-full.txt) shows. To remove the protection in code, set `EditMode` to `EditMode.Edit`: for a document protected with a password, the editor asks for it.

### Check What the User May Do

Read-only properties of the `TextControl` tell whether an action is allowed at the moment, taking the edit mode and the permissions into account. Use them to enable or disable buttons and menu items of your own:

| Property | Tells whether |
|---|---|
| [CanEdit](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canedit-property/llms-full.txt) | The text or its formatting can be changed |
| [CanPrint](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canprint-property/llms-full.txt) | The document can be printed |
| [CanCopy](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/cancopy-property/llms-full.txt) | Text is selected and may be copied |
| [CanCharacterFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/cancharacterformat-property/llms-full.txt), [CanParagraphFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canparagraphformat-property/llms-full.txt) | The selection can be formatted |
| [CanStyleFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canstyleformat-property/llms-full.txt) | Styles can be applied |
| [CanDocumentFormat](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/candocumentformat-property/llms-full.txt) | Page and section formatting, such as margins and page size, can be changed |
| [CanEditFormFields](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/caneditformfields-property/llms-full.txt) | Form fields can be filled in |

### Work With Editable Regions

- [GetItems](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/getitems-method/llms-full.txt) returns the regions at the input position as an array, or `null`.
- [Remove](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioncollection/remove-method/llms-full.txt) removes a region but not its text. With `true` as the second parameter, only the selected part of the region is removed, which can split it in two.
- [EditableRegionHighlightMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregionhighlightmode-property/llms-full.txt) of the `TextControl` highlights the regions while they contain the input position (`HighlightMode.Activated`, the default), always or never. A region's own [HighlightMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregion/highlightmode-property/llms-full.txt) and [HighlightColor](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregion/highlightcolor-property/llms-full.txt) override it; the color needs an alpha value below 255 so that the text remains visible.
- [ScrollTo](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregion/scrollto-method/llms-full.txt) moves the input position to a region, and [Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregion/text-property/llms-full.txt) returns its text.

The `TextControl` raises [EditableRegionCreated](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregioncreated-event/llms-full.txt) and [EditableRegionDeleted](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregiondeleted-event/llms-full.txt) when a region is added or deleted, and [EditableRegionEntered](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregionentered-event/llms-full.txt) and [EditableRegionLeft](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/editableregionleft-event/llms-full.txt) when the input position moves into or out of one. Their handlers receive [EditableRegionEventArgs](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioneventargs/llms-full.txt) with the region in [EditableRegion](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/editableregioneventargs/editableregion-property/llms-full.txt):

```csharp
private void textControl1_EditableRegionEntered(object sender, EditableRegionEventArgs e)
{
    string user = string.IsNullOrEmpty(e.EditableRegion.UserName) ? "everyone" : e.EditableRegion.UserName;
    Text = $"Editable by {user}";
}
```

Headers, footers and text frames have regions of their own, in the `EditableRegions` collections of [HeaderFooter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/headerfooter/editableregions-property/llms-full.txt) and [TextFrame](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textframe/editableregions-property/llms-full.txt). To save a document without the region markers, keeping their text, pass a [SaveSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/llms-full.txt) object whose [OmittedContent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/omittedcontent-property/llms-full.txt) is `OmittedContent.EditableRegions` to `Save`.

### Next Steps

- [Document Protection](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/security/document-protection/llms-full.txt): what protection does and doesn't guard against.
- [Form Fields and Conditional Instructions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/form-fields/llms-full.txt): forms whose fields remain editable in a protected document.
- [Passwords and Access Permissions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/pdf/passwords/llms-full.txt): passwords and permissions of PDF files.
- [Track Changes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/track-changes/llms-full.txt): record changes with the author from `UserNames`.

---

## Nearby

- **Previous:** [Comments](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/comments/llms-full.txt)
- **Next:** [Spell Checking and Hyphenation](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/spell-checking/llms-full.txt)
