C#: Check Whether a PDF is Password Protected and Determine the Correct Password

Password protection is a widely used security feature in PDFs to restrict access and prevent unauthorized modifications. Before working with a PDF, it is essential to determine whether it is password-protected. If protection is enabled, verifying the correct password allows you to unlock the document, ensuring smooth access for viewing, editing, or extracting its contents.

In this article, we will guide you through the process of checking whether a PDF is password-protected and how to verify the correct password using C# and the Spire.PDF for .NET library.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Check Whether a PDF is Password Protected in C#

Spire.PDF for .NET provides the PdfDocument.IsPasswordProtected(string fileName) method to determine whether a PDF file is password-protected. The detailed steps are as follows.

  • Specify the input and output file paths.
  • Use the PdfDocument.IsPasswordProtected(string fileName) method to check whether the PDF is password protected.
  • Save the verification result to a text file.
  • C#
using Spire.Pdf;
using System.IO;

namespace CheckIfPdfIsProtected
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Specify the input and output file paths
            string pdfPath = "sample.pdf";
            string resultFilePath = "verification_results.txt";

            // Check whether the PDF file is password-protected
            bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);

            // Create a StreamWriter to write the result to a text file
            using (StreamWriter writer = new StreamWriter(resultFilePath))
            {
                // Write the verification result to the text file
                string resultMessage = isProtected ? "The PDF is password-protected." : "The PDF is not password-protected.";
                writer.WriteLine(resultMessage);
            }
        }
    }
}

Check Whether a PDF is Password Protected in C#

Determine the Correct Password of a PDF in C#

Spire.PDF for .NET does not have a direct method to verify if a password is correct, but this can be done by attempting to open the file with the given password. If the password is incorrect, an exception will be thrown. The detailed steps are as follows.

  • Specify the input and output file paths.
  • Check whether the PDF file is password-protected using the PdfDocument.IsPasswordProtected(string fileName) method.
  • Create an array of potential passwords to test.
  • Iterate through the array, and load the PDF with each password using the PdfDocument.LoadFromFile(string filename, string password) method.
  • If no exception is thrown, the password is correct. Otherwise, the password is incorrect.
  • Save the verification result to a text file.
  • C#
using Spire.Pdf;
using System;
using System.IO;

namespace DetermineTheCorrectPasswordOfPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Specify the input and output file paths
            string pdfPath = "sample.pdf";
            string resultFilePath = "verification_results.txt";

            // Check whether the PDF file is password-protected
            bool isProtected = PdfDocument.IsPasswordProtected(pdfPath);

            // Create an array of potential passwords to test
            string[] passwords = new string[5] { "password1", "password2", "password3", "admin123", "test" };

            // Create a StreamWriter to write results to a text file
            using (StreamWriter writer = new StreamWriter(resultFilePath))
            {
                // If the PDF is protected, start testing passwords
                if (isProtected)
                {
                    // Iterate through each password in the array
                    for (int passwordcount = 0; passwordcount < passwords.Length; passwordcount++)
                    {
                        try
                        {
                            // Create a new PdfDocument object and try loading the document with the current password
                            PdfDocument doc = new PdfDocument();
                            doc.LoadFromFile(pdfPath, passwords[passwordcount]);

                            // If successful, write that the password is correct to the text file
                            writer.WriteLine("Password " + passwords[passwordcount] + " is correct");
                        }
                        catch
                        {
                            // If an exception occurs, write that the password is not correct to the text file
                            writer.WriteLine("Password " + passwords[passwordcount] + " is not correct");
                        }
                    }
                }
                else
                {
                    // If the PDF is not password protected, note this in the text file
                    writer.WriteLine("The PDF is not password protected.");
                }
            }

            Console.WriteLine("Verification results have been saved to: " + resultFilePath);
            Console.ReadKey();
        }
    }
}

Determine the Correct Password of a PDF in C#

Get a Free License

To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.