# Document Protection and User Permissions

> Know what protected documents in the WPF TextControl prevent and allow, assign UserNames from your sign-in and unlock files in code with MasterPassword.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Security > Document Protection and User Permissions
- **2076 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/document-protection/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/document-protection/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/document-protection/llms-full.txt

---

Document protection answers one question well: what may a user change in this document, in this editor? A protected document in the WPF `TextControl` limits editing and formatting, and its editable regions stay open for everyone or for a named user. It answers other questions poorly or not at all: who may read the document, whether it was changed and who the user really is. Where the protection ends, and how your application fills the gaps, is the subject here.

Setting up protection with the **Permissions** tab and in code is shown in [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/llms-full.txt). The code below belongs in *MainWindow.xaml.cs* of a window with a `TextControl` named `textControl1`.

### How a Document Is Protected

[EditMode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/editmode-property/llms-full.txt) set to `ReadAndSelect`, optionally combined with `UsePassword`, switches the restrictions on, [DocumentPermissions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/documentpermissions-property/llms-full.txt) sets what stays allowed, and editable regions remain open for one user name or for all users. RTF, DOC, DOCX, PDF and the TX Text Control format keep the permissions and the password in encrypted form, which Text Control calls compatible with Microsoft Word. [How Document Protection Works](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/#how-document-protection-works) describes the edit modes and each permission.

### What It Keeps From Happening

- **Unwanted changes in the editor.** Outside the regions of the current user, the `TextControl` allows only what the permissions allow.
- **Lifting the protection without the password.** A document protected with a password is opened read-only, and switching the edit mode back to `Edit` requires the same password.
- **Commands that shouldn't be offered.** For your own buttons and menu items, [CanEdit](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canedit-property/llms-full.txt), [CanPrint](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canprint-property/llms-full.txt) and [CanCopy](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/cancopy-property/llms-full.txt) say whether the action is allowed at the moment.

### What It Leaves Open

- **Reading.** Protection restricts changes, not access: a protected document opens read-only, and its text can be read and selected. Confidential content needs a PDF with a user password, as [PDF Passwords and Permissions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/pdf-passwords/llms-full.txt) explains.
- **Restrictions without a password.** According to Text Control's article on document permissions, the permissions are saved in the document only when `EditMode` is `ReadAndSelect` together with `UsePassword`. Restrictions set without a password last only as long as the document is open, and setting `EditMode` to `Edit` removes them.
- **Access for everyone who has the password.** The protection password serves as the master password of the document. Loading the document with it in [MasterPassword](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsavesettingsbase/masterpassword-property/llms-full.txt) of the `LoadSettings` removes all restrictions.
- **Evidence of changes.** Nothing in a protected document shows whether someone with the password edited it. For that, sign the PDF you hand out, as [Certificate and Private Key Handling](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/certificates/llms-full.txt) describes.

### Set User Names From Your Sign-In

[UserNames](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/usernames-property/llms-full.txt) lists the users who have access to editable regions: when a document is read-only, the regions of these users can be edited. The `TextControl` takes the names as they are. It has no sign-in of its own, so a name typed into a box and passed on unchecked opens that user's regions to anybody.

The WPF version of the TX Words sample *User Access* demonstrates it. As long as nobody is signed in, only the region for all users is highlighted and can be changed:

![The WPF Permissions tab with Enforce Protection active and only the green paragraph for all users highlighted.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/permissions-signed-out.8e3e3079.webp)

Its **Sign In** command opens a password dialog box of the sample, in which a new user account is created on the first sign-in:

![The Create New User Account dialog box of the User Access sample for the user John, with the new password entered twice and the buttons OK and Cancel.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/create-user-account-dialog.d20f789d.webp)

After a successful sign-in, the sample assigns the signed-in user's name to `UserNames`. John's region is now highlighted and editable too:

![The same WPF document after signing in as John, with the words general section highlighted as John's region.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-wpf/permissions-signed-in.5e508c04.webp)

Do the same in your application: take the name from the user your application authenticated, and assign it before the document is loaded:

```csharp
private void OpenForSignedInUser(string signedInUserName)
{
    // The name comes from your application's sign-in, not from a text box
    textControl1.UserNames = new string[] { signedInUserName };

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

Call the method once the `TextControl` has raised its `Loaded` event, for example from the handler of a menu command. The first name is also the author of new comments and tracked changes. In the **Add Users** dialog box of the **Permissions** tab, [RegisteredUserNames](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribbonpermissionstab/registeredusernames-property/llms-full.txt) and [AllowAddingUserNames](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/ribbonpermissionstab/allowaddingusernames-property/llms-full.txt) restrict the names to those of your application, as [Protect a Document With the Ribbon](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/#protect-a-document-with-the-ribbon) describes.

### Unlock Protected Documents in Code

Code needs the protection password just as users do. Text Control's articles pass it in `MasterPassword` to load a protected document without restrictions, for example to save it as PDF with a [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt):

```csharp
private void ExportContractButton_Click(object sender, RoutedEventArgs e)
{
    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        // The password of the protection lifts all restrictions
        LoadSettings loadSettings = new LoadSettings()
        {
            MasterPassword = protectionPassword
        };

        tx.Load("contract.tx", StreamType.InternalUnicodeFormat, loadSettings);
        tx.Save("contract.pdf", StreamType.AdobePDF);
    }
}
```

The code works as follows:

- [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes the `ServerTextControl`; it's called before any other member.
- `protectionPassword` is a field of the window. Ask for the password or read it from a protected store when you need it; a password written into the source code is part of every installed copy of your application.
- Without the password, the loaded document remains protected, and the article reports that it can't be exported to PDF as it is.

The reference documents `MasterPassword` as a setting for PDF documents; its use for other protected formats is taken from the articles [Document Permissions and Password Encryption](https://www.textcontrol.com/blog/2019/07/05/document-permissions-and-password-encryption/) and [Exporting Password Protected Documents to PDF in .NET C#](https://www.textcontrol.com/blog/2024/10/28/exporting-password-protected-documents-to-pdf-in-net-c-sharp/).

### Which Protection Fits

| You need | Choose |
|---|---|
| Parts of a document that only certain users of your editor may change | Document protection with editable regions |
| A document that can't be read without a password | A PDF with a user password |
| Proof for recipients that nothing was changed | A digitally signed PDF |
| Limits on printing or copying in PDF readers | PDF permissions with a master password, and a user password when the content is confidential |

### Next Steps

- [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/llms-full.txt): the ribbon and the code for protected documents.
- [PDF Passwords and Permissions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/pdf-passwords/llms-full.txt): protection for documents that leave your application.
- [Form Fields and Conditional Instructions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/form-fields/llms-full.txt): fields that stay editable in protected forms.
- [Security Overview](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/overview/llms-full.txt): the other protections and what your application decides.

---

## Nearby

- **Previous:** [Security Overview](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/overview/llms-full.txt)
- **Next:** [Handling Untrusted Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/untrusted-documents/llms-full.txt)
