PDF encryption is a key requirement in many applications where sensitive documents need protection from unauthorized access. With TX Text Control, you can encrypt PDF documents with a user-defined password and programmatically verify that the encryption is working properly, all within a few lines of C# code.

This article will show you how to create an encrypted PDF document and verify the encryption by attempting to open the file under different conditions.

Saving an Encrypted PDF

TX Text Control offers several encryption options for saving PDF documents. With the SaveSettings.UserPassword TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSaveSettingsBase Class
UserPassword Property
Specifies the password for the user when the document is reopened.
property, you can set a password that is required to open the document. Below is a short example that saves a PDF document with password protection:

using var serverTextControl = new ServerTextControl();
serverTextControl.Create();
serverTextControl.Text = "This is a sample document.";
var saveSettings = new SaveSettings {
UserPassword = "user1234"
};
serverTextControl.Save("EncryptedDocument.pdf", StreamType.AdobePDF, saveSettings);
view raw test.cs hosted with ❤ by GitHub

How do you verify that the encryption actually worked? Sure, you can open the PDF in Acrobat Reader. When prompted for a password, the document will be successfully signed. However, we want to ensure that the process has been completed successfully programmatically.

Verifying the Encryption

To ensure the PDF is properly encrypted, attempt to load it without a password. If it fails, the file is encrypted. If it fails with error code 1-1D10, then we know the file is encrypted. Then, we can retry loading the file with the correct password to see if it succeeds. We've wrapped this process into a reusable method.

using TXTextControl;
public static class PdfEncryptionHelper
{
public static bool SaveAndVerifyPdfEncryption(ServerTextControl sourceTextControl, string userPassword, string destinationPath)
{
// Save the document encrypted
var saveSettings = new SaveSettings
{
UserPassword = userPassword
};
try
{
sourceTextControl.Save(destinationPath, StreamType.AdobePDF, saveSettings);
Console.WriteLine($"PDF saved with encryption at: {destinationPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to save PDF: {ex.Message}");
return false;
}
// Try to load the PDF without a password (should fail)
using var verificationControl = new ServerTextControl();
verificationControl.Create();
try
{
verificationControl.Load(destinationPath, StreamType.AdobePDF);
Console.WriteLine("Unexpected: PDF loaded without a password.");
return false; // Not encrypted properly
}
catch (FilterException exc)
{
if (!exc.Message.Contains("1-1D10"))
{
Console.WriteLine($"Unexpected error when loading PDF: {exc.Message}");
return false;
}
// Expected: encryption detected, now try with password
try
{
var loadSettings = new LoadSettings
{
UserPassword = userPassword
};
verificationControl.Load(destinationPath, StreamType.AdobePDF, loadSettings);
Console.WriteLine("PDF decrypted and loaded successfully.");
return true;
}
catch (FilterException loadExc)
{
Console.WriteLine($"Decryption failed: {loadExc.Message}");
return false;
}
}
}
}
view raw test.cs hosted with ❤ by GitHub

Usage

To use the above method, simply call it with the path to your PDF file and the password you set when saving the document. If the method returns true, the encryption is verified successfully.

using var serverTextControl = new ServerTextControl();
serverTextControl.Create();
serverTextControl.Text = "This is a sample document.";
bool success = SaveAndVerifyPdfEncryption(serverTextControl, "user1234", "EncryptedDocument.pdf");
Console.WriteLine($"Encryption verification result: {success}");
view raw test.cs hosted with ❤ by GitHub

Conclusion

TX Text Control simplifies the process of encrypting PDF documents and verifying that the encryption is functioning properly. Using the provided methods ensures that your sensitive documents are protected against unauthorized access.