How to Verify PDF Encryption Programmatically in C# .NET
This article explains how to programmatically verify the encryption of a PDF document using C# .NET. It provides the necessary steps and code examples to ensure the PDF is encrypted correctly, offering a secure method for handling sensitive documents within your applications.

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 Save
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);
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;
}
}
}
}
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}");
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.
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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
Related Posts
PDF Security for C# Developers: Encryption and Permissions in .NET
In this article, we will explore how to implement PDF security in .NET applications using TX Text Control. We discuss encryption methods and permission settings that protect sensitive information…
A Complete Guide to Converting Markdown to PDF in .NET C#
Learn how to convert Markdown to PDF in .NET C# using Text Control's ServerTextControl component. This guide covers setup, conversion process, and customization options for generating high-quality…
ASP.NETASP.NET CoreDocument Creation
Why PDF Creation Belongs at the End of the Business Process
This article discusses why placing PDF creation at the end of the business process is important for ensuring accuracy and efficiency. The most scalable systems delay PDF generation until the…
Designing the Perfect PDF Form with TX Text Control in .NET C#
Learn how to create and design interactive PDF forms using TX Text Control in .NET C#. This guide covers essential features and best practices for effective form design.
Why Defining MIME Types for PDF/A Attachments Is Essential
The PDF/A standard was created to ensure the long-term reliable archiving of digital documents. An important aspect of the standard involves properly handling embedded files and attachments within…
