With TX Text Control, you can protect documents from authoring by encrypting the document with a password and adding editable areas as exceptions for specific users. The next code snippet adds an editable region in a specific selection range for the user developer@textcontrol.com. The word Text is now editable for this specific user:

textControl1.Text = "TX Text Control";
EditableRegion region = new EditableRegion("developer@textcontrol.com", 10, 4, 4);
textControl1.EditableRegions.Add(region);
view raw tx.cs hosted with ❤ by GitHub

Protecting Documents

The following code can be used to protect and save the document:

textControl1.EditMode = EditMode.UsePassword | EditMode.ReadAndSelect;
textControl1.Save("encrypted-document.tx", TXTextControl.StreamType.InternalUnicodeFormat);
view raw tc.cs hosted with ❤ by GitHub

After loading the document, the UserNames TX Text Control .NET Server for ASP.NET
Web Namespace
TextControl Class
UserNames Property
Gets or sets a list of names specifying users who have access to editable regions.
property can be used to define a list of names that specify users who have access to editable areas. In this example, we grant access to the user developer@textcontrol.com:

@Html.TXTextControl().TextControl(settings => {
settings.UserNames = new string[] { "developer@textcontrol.com" };
}).LoadText(sDocument, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat).Render()
view raw test.cshtml hosted with ❤ by GitHub

Loading Secured Documents

But what if you want to load a secured document into a non-visual ServerTextControl TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications.
and export the document to PDF?

Because the loaded document is protected and cannot be exported to PDF as is, the following code would fail:

using TXTextControl;
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
// Load an encrypted document protected with a password
tx.Load("encrypted-document.tx", StreamType.InternalUnicodeFormat);
tx.Save("results.pdf", StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

Unlocking Secured Documents

There are two ways to solve this problem. The first way is to provide the password when loading the document to unlock it. This is done using the MasterPassword TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSaveSettingsBase Class
MasterPassword Property
Specifies a password for the document's access permissions.
property passed in the Load TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
Load Method
Loads text in a certain format.
method.

using TXTextControl;
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
LoadSettings loadSettings = new LoadSettings()
{
MasterPassword = "123"
};
// Load an encrypted document protected with a password
tx.Load("encrypted-document.tx", StreamType.InternalUnicodeFormat, loadSettings);
tx.Save("results.pdf", StreamType.AdobePDF);
}
view raw test.cs hosted with ❤ by GitHub

Preserving Original Settings

However, if you want to preserve the original author's DocumentAccessPermissions TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
SaveSettings Class
DocumentAccessPermissions Property
Specifies how a document can be accessed after it has been opened.
, you can set the password when saving the PDF. The following code shows an advanced logic that uses the FilterExceptions TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
FilterException Class
The FilterException class informs about errors which can occur when a text filter is used to convert a document to or from another format.
when saving the document. If the document contains an encrypted password, an exception is thrown and the MasterPassword TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSaveSettingsBase Class
MasterPassword Property
Specifies a password for the document's access permissions.
are used to pass the password to the Save TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
Save Method
Saves the complete contents of a document with the specified format.
method.

using TXTextControl;
using (ServerTextControl tx = new ServerTextControl()) {
tx.Create();
// Load an encrypted document protected with a password
tx.Load("encrypted-document.tx", StreamType.InternalUnicodeFormat);
// Trying to save the document as PDF
try {
tx.Save("results.pdf", StreamType.AdobePDF);
}
catch (FilterException exc) {
// Check if the exception was thrown because of an encrypted password
if (exc.Reason == FilterException.FilterError.EncryptedPassword)
{
// Provide the password to save the document
SaveSettings saveSettings = new SaveSettings() {
MasterPassword = "123"
};
// Save the document as PDF
tx.Save("results.pdf", StreamType.AdobePDF, saveSettings);
}
}
}
view raw test.cs hosted with ❤ by GitHub

Conclusion

With TX Text Control, you can protect documents from editing by encrypting the document with a password and adding editable areas as exceptions for certain users. This article shows how to protect documents and how to unlock protected documents for further processing and export to PDF.