# Document Protection

> Mark EditableRegion ranges for all or single users with ServerTextControl, read what they entered, remove them and set DocumentPermissions in TextViewGenerator.

- **Product:** TX Text Control .NET Server
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET Server > Document Processing > Document Protection
- **3548 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/protection/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/protection/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/protection/llms-full.txt

---

A contract that customers may complete only in a few places is protected: its text can be read and selected, and only marked passages can be changed. The protection itself applies in the Document Editor, while the server prepares and processes the documents. Your code marks editable regions for everyone or for single users, reads the text users entered there and removes the regions before the document is finalized.

How users protect a document with a password and which permissions a protected document has is the subject of [Document Protection](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-editor/protection/llms-full.txt) in the Document Editor section. Passwords and access permissions of PDF files are a different mechanism, set when a document is saved as PDF.

### How Document Protection Works

A protected document is opened in the Document Editor with the edit mode `ReadAndSelect`. In this mode, the text is read-only, and the permissions stored in the document apply, for example whether text can be formatted or printed. Editable regions are the exceptions:

- **An editable region** is a range of text that stays editable in a protected document. It's editable by everyone or only by the user whose name the region carries.
- **The users** who work with the editor are passed to it with [TextControlSettings.UserNames](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-web-mvc/textcontrolsettings/usernames-property/llms-full.txt). A user can change the regions marked with their name and the regions without a name.
- **Regions can overlap.** A region that overlaps or directly follows another region with the same user name and ID is combined with it.

`ServerTextControl` has no edit mode and no document permissions, so passwords and permissions are set in the Document Editor. On the server, you work with the editable regions: [ServerTextControl.EditableRegions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/editableregions-property/llms-full.txt) returns the [EditableRegionCollection](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/llms-full.txt) of the main text, and [HeaderFooter.EditableRegions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/headerfooter/editableregions-property/llms-full.txt) and [TextFrame.EditableRegions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/textframe/editableregions-property/llms-full.txt) those of a header, footer or text frame.

### What the Document Permissions Are

The permissions of a protected document, such as whether it can be formatted, copied or printed, are the properties of the [DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/documentpermissions/llms-full.txt) class, which [The Document Permissions](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-editor/protection/#the-document-permissions) in the Document Editor lists. All of them are `true` by default, including `ReadOnly`, `AllowFormattingStyles` works only together with `AllowFormatting`, and the permissions take effect when the edit mode changes from `Edit` to `ReadAndSelect`. A document protected with a password keeps the permissions and the password in encrypted form in RTF, DOC, DOCX, PDF and the TX Text Control format, and it opens read-only.

`ServerTextControl` has no property that returns a `DocumentPermissions` object. On the server, the class is reachable through [TextViewGenerator.DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-servervisualization/textviewgenerator/documentpermissions-property/llms-full.txt) together with [TextViewGenerator.EditMode](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-servervisualization/textviewgenerator/editmode-property/llms-full.txt), a special kind of `ServerTextControl` that renders the document as images. In the Document Editor, users set the permissions on the **Permissions** tab, as [Document Protection](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-editor/protection/llms-full.txt) shows.

### Mark Editable Regions

The following program writes a short contract with a header, marks the customer name as editable for everyone, the approver as editable for one user and a reference number in the header as editable for everyone. It saves the document in the TX Text Control format:

```csharp
using TXTextControl;

using (ServerTextControl tx = new ServerTextControl())
{
    tx.Create();

    tx.Text = "Service Agreement\r\n" +
        "Customer: [Customer name]\r\n" +
        "Approved by: [Approver]\r\n" +
        "The terms of this agreement are fixed.";

    // An empty user name makes a region editable for everyone
    AddRegion(tx.EditableRegions, tx.Find("[Customer name]"), "[Customer name]", "", 1);
    AddRegion(tx.EditableRegions, tx.Find("[Approver]"), "[Approver]", "Paul Proof", 2);

    tx.HeadersAndFooters.Add(HeaderFooterType.Header);
    HeaderFooter header = tx.HeadersAndFooters.GetItem(HeaderFooterType.Header);
    header.Selection.Text = "Reference: [Reference]";

    AddRegion(header.EditableRegions, header.Find("[Reference]", 0, FindOptions.MatchCase), "[Reference]", "", 3);

    tx.Save("Documents/contract.tx", StreamType.InternalUnicodeFormat);
}

static void AddRegion(EditableRegionCollection regions, int position, string text, string userName, int id)
{
    if (position == -1)
    {
        Console.WriteLine($"{text} wasn't found.");
        return;
    }

    // Find returns a zero-based position, the region expects a one-based start
    EditableRegion region = new EditableRegion(userName, id, position + 1, text.Length);
    EditableRegionCollection.AddResult result = regions.Add(region);

    Console.WriteLine($"{text}: {result}");
}
```

The code works as follows:

- [ServerTextControl.Find](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/find-method/llms-full.txt) and [HeaderFooter.Find](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/headerfooter/find-method/llms-full.txt) return the zero-based position of the text in the main text and in the header, or -1. `HeaderFooter.Find` has one overload only, which also takes the position to start at and a value of `FindOptions`.
- The [EditableRegion constructor](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/constructor/llms-full.txt) takes the user name, a one-based ID, the one-based start position and the length. A user name that is `null` or empty means everyone, and the ID 0 means no ID. Without a start position and length, `Add` uses the current selection.
- [EditableRegionCollection.Add](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/add-method/llms-full.txt) returns an [AddResult](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/addresult-enumeration/llms-full.txt): `Successful`, `Combined` when the region was merged with another one, `PositionInvalid`, `NoSelection`, `SelectionTooComplex` or `Error`.
- The document is saved in the TX Text Control format, in which the Document Editor loads it with its editable regions.

The document now has three editable regions but no protection yet. When it's opened for Paul Proof in `ReadAndSelect`, as [Open a Protected Document](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-editor/protection/#open-a-protected-document) shows, Paul Proof can change the customer name, the approver and the reference, and the rest of the text is read-only.

### Read What Users Entered

After users have edited their regions and the document is back on the server, the regions tell you what they entered. The following program lists all regions, saves the text of the approver region as HTML and saves a final DOCX without the regions:

```csharp
using TXTextControl;

using (ServerTextControl tx = new ServerTextControl())
{
    tx.Create();
    tx.Load("Documents/contract.tx", StreamType.InternalUnicodeFormat);

    foreach (IFormattedText textPart in tx.TextParts)
    {
        foreach (EditableRegion region in textPart.EditableRegions)
        {
            string user = string.IsNullOrEmpty(region.UserName) ? "everyone" : region.UserName;
            Console.WriteLine($"Region {region.ID} ({user}) at {region.Start}: \"{region.Text}\"");
        }
    }

    // null if no region of the main text has the ID 2
    EditableRegion approver = tx.EditableRegions.GetItem(2);

    if (approver != null)
    {
        approver.Save(out string html, StringStreamType.HTMLFormat);
        File.WriteAllText("approver.html", html);

        // false: the complete region is removed, its text stays
        tx.EditableRegions.Remove(approver, false);
    }

    // Leave out the markers of the remaining regions, keep their text
    SaveSettings saveSettings = new SaveSettings()
    {
        OmittedContent = OmittedContent.EditableRegions
    };

    tx.Save("contract-final.docx", StreamType.WordprocessingML, saveSettings);
}
```

The code works as follows:

- [ServerTextControl.TextParts](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/textparts-property/llms-full.txt) contains the main text, the text frames and the headers and footers, each with its [EditableRegions](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/iformattedtext/editableregions-property/llms-full.txt).
- [UserName](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/username-property/llms-full.txt), [ID](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/id-property/llms-full.txt), [Start](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/start-property/llms-full.txt) and [Text](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/text-property/llms-full.txt) describe a region. [Number](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/number-property/llms-full.txt) counts the regions in the text flow from 1.
- [GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/getitem-method/llms-full.txt) returns the first region with an ID, or `null`. [GetItems](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/getitems-method/llms-full.txt) returns the regions at the input position.
- [EditableRegion.Save](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregion/save-method/llms-full.txt) writes the text of a region to a string or a byte array in a format you choose.
- [EditableRegionCollection.Remove](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioncollection/remove-method/llms-full.txt) removes a region but not its text. With `true` as the second parameter, it removes only the part of the region that is selected, which can split the region in two.
- `OmittedContent.EditableRegions` saves the document without its editable regions; the text and its formatting are saved. This setting can't be used for PDF.

### Events of Editable Regions

[EditableRegionCreated](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/editableregioncreated-event/llms-full.txt) and [EditableRegionDeleted](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/editableregiondeleted-event/llms-full.txt) occur when a region is created or deleted. The [EditableRegionEventArgs](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/editableregioneventargs/llms-full.txt) contain the region; in an `EditableRegionDeleted` handler, only its `UserName`, `ID` and highlight settings are still valid. [Events](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/events/llms-full.txt) lists all events of `ServerTextControl`.

Single fields can be protected as well: [TextField.Editable](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/textfield/editable-property/llms-full.txt) and [Deleteable](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/textfield/deleteable-property/llms-full.txt) decide whether users can change or delete a field, as [Fields](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/fields/llms-full.txt) describes.

The blog article [Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases](https://www.textcontrol.com/blog/2025/06/25/document-protection-in-asp-dotnet-with-tx-text-control-healthcare-use-cases/) combines editable regions, users and the Document Editor in an application.

### Next Steps

- [Document Protection](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-editor/protection/llms-full.txt) in the Document Editor: protect documents with permissions and passwords.
- [Form Fields](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/form-fields/llms-full.txt): create forms that users fill out.
- [Subtext Parts](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/subtext-parts/llms-full.txt): mark ranges of text for your own code.
- [Document Security](https://docs.textcontrol.com/tx-text-control-dotnet-server/security/documents/llms-full.txt): protect documents on the server.

---

## Nearby

- **Previous:** [Track Changes](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/track-changes/llms-full.txt)
- **Next:** [Spell Checking and Hyphenation](https://docs.textcontrol.com/tx-text-control-dotnet-server/document-processing/spell-checking/llms-full.txt)
