How to Detect if a PDF Is Encrypted Using C# (Free Library)

When processing PDF files in bulk, encountering an encrypted PDF often crashes your application. The file throws an exception the moment you try to read, edit, or extract text from it. The solution? Check for encryption before you load the document.

In this tutorial, you’ll learn how to detect whether a PDF is password‑protected using C# and the Free Spire.PDF for .NET library – with just a few lines of code.

Why Detect PDF Encryption?

  • Avoid runtime crashes – Prevent exceptions when batch processing encrypted PDFs.

  • Improve user experience – Warn users that a file is password‑protected before opening.

  • Simplify security audits – Keep a log of which PDFs in your system are encrypted.

What You’ll Need

  • Visual Studio (or any C# IDE)

  • .NET Framework 4.0+ or .NET Core/.NET 5+

  • Free Spire.PDF for .NET library

Installing Free Spire.PDF (NuGet)

Open your NuGet Package Manager Console and run:

Install-Package FreeSpire.PDF

The free version is fully sufficient for detecting PDF encryption.

Core Method: IsPasswordProtected

The PdfDocument class provides a static method that returns true if the PDF is encrypted, and false otherwise.

bool isProtected = PdfDocument.IsPasswordProtected("yourfile.pdf");

That’s it. No need to provide a password or parse the PDF structure manually.

Full C# Example – Detect PDF Encryption

using Spire.Pdf;
using System;

namespace PdfEncryptionDetector
{
    class Program
    {
        static void Main(string[] args)
        {
            string pdfPath = "sample.pdf";

            // Detect if the PDF is password-protected
            bool isEncrypted = PdfDocument.IsPasswordProtected(pdfPath);

            if (isEncrypted)
                Console.WriteLine("This PDF is encrypted (password-protected).");
            else
                Console.WriteLine("This PDF is not encrypted.");
        }
    }
}

Performance & Error Handling Tips

  • Fast metadata read – The method only scans the PDF header and encryption dictionary, so it works quickly even on large files.

  • Handle missing files – Always wrap the call in a try-catch block:

try
{
    bool result = PdfDocument.IsPasswordProtected(pdfPath);
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}"); // file not found or corrupted
}
  • No password required – The detection works without knowing the actual password.

Limitations of the Free Version

Free Spire.PDF has a 10‑page limit for operations like text extraction, conversion, or printing. However, the IsPasswordProtected method is exempt from this limit. You can safely check encryption on PDFs of any size.

How to Open an Encrypted PDF After Detection (Optional)

Once you know a PDF is encrypted, you can ask the user for the password and load it:

if (PdfDocument.IsPasswordProtected(pdfPath))
{
    Console.Write("Enter password: ");
    string password = Console.ReadLine();

    using (PdfDocument doc = new PdfDocument())
    {
        try
        {
            doc.LoadFromFile(pdfPath, password);
            Console.WriteLine("PDF loaded successfully.");
            // Now you can read text, extract images, etc.
        }
        catch
        {
            Console.WriteLine("Wrong password.");
        }
    }
}

Complete Batch Detection Example (Console App)

The following snippet checks all PDFs in a folder and writes the results to a log file:

using Spire.Pdf;
using System;
using System.IO;

class BatchPdfChecker
{
    static void Main()
    {
        string folder = @"C:\PDFs";
        string logFile = "encryption_log.txt";

        foreach (string file in Directory.GetFiles(folder, "*.pdf"))
        {
            bool encrypted = PdfDocument.IsPasswordProtected(file);
            File.AppendAllText(logFile, $"{Path.GetFileName(file)}: {(encrypted ? "Encrypted" : "Open")}\n");
        }
        Console.WriteLine("Done. Check encryption_log.txt");
    }
}

Summary

Detecting PDF encryption in C# doesn’t have to be complicated. With Free Spire.PDF and the IsPasswordProtected method, you can:

  • ✅ Check password protection in one line of code

  • ✅ Avoid crashes in batch processing

  • ✅ Integrate encryption detection into any .NET application (Console, WinForms, ASP.NET)

Try the code above in your next project. Your error logs will thank you.