Products Technologies Demo Docs Blog Support Company

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.

Exporting Password Protected Documents to PDF in .NET C#

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 UserNames 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()

Loading Secured Documents

But what if you want to load a secured document into a non-visual ServerTextControl 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);
}

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 property passed in the Load 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);
}

Preserving Original Settings

However, if you want to preserve the original author's DocumentAccessPermissions, you can set the password when saving the PDF. The following code shows an advanced logic that uses the FilterExceptions when saving the document. If the document contains an encrypted password, an exception is thrown and the MasterPassword are used to pass the password to the Save 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);
        }
    }
}

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.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETAIASP.NET Core

Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…

This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…


ASP.NETASP.NET CoreJava

Converting Office Open XML (DOCX) to PDF in Java

Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…


ASP.NETASP.NET CoreDS Server

Extending DS Server with Custom Digital Signature APIs

In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…


ASP.NETASP.NET CorePDF

Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance

It is more important than ever to ensure that documents are accessible, archivable, and legally compliant. PDF/UA and PDF/A-3a are two effective standards for addressing these needs. This article…


ASP.NETASP.NET CoreMarkdown

Convert Markdown to PDF in a Console Application on Linux and Windows

Learn how to convert Markdown files to PDF in a console application on Linux and Windows using TX Text Control .NET Server for ASP.NET. This tutorial provides step-by-step instructions and code…