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?

What You’ll Need

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

try
{
    bool result = PdfDocument.IsPasswordProtected(pdfPath);
}
catch (Exception ex)
{
    Console.WriteLine($"Error: {ex.Message}"); // file not found or corrupted
}

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:

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