Exporting Password Protected Documents to PDF in .NET C#
This article shows how to export password protected documents to PDF in .NET C# using TX Text Control .NET Server. Existing password protected documents can be imported, modified and exported to PDF with a password.

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);
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);
After loading the document, the User
@Html.TXTextControl().TextControl(settings => {
settings.UserNames = new string[] { "developer@textcontrol.com" };
}).LoadText(sDocument, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat).Render()
Loading Secured Documents
But what if you want to load a secured document into a non-visual Server
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);
}
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 Master
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);
}
Preserving Original Settings
However, if you want to preserve the original author's Document
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);
}
}
}
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.
Related Posts
Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas
Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…
Streamline Data Collection with Embedded Forms in C# .NET
Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…
Adding QR Codes to PDF Documents in C# .NET
This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…
Adding SVG Graphics to PDF Documents in C# .NET
In this article, we will explore how to add SVG graphics to PDF documents using C# .NET. We will use the TX Text Control .NET Server component to demonstrate the process of rendering SVG images in…
Enhancing PDF Searchability in Large Repositories by Adding and Reading…
This article explores how to improve the searchability of PDF documents in large repositories by adding and reading keywords with C# .NET. This is especially helpful for applications that manage…