Detect if the PDF file is password protected in C#/VB.NET

This article demonstrates how to detect whether a PDF file is encrypted or not by using Spire.PDF for .NET. Spire.PDF offers a method named IsPasswordProtected(string fileName) which returns a boolean value. If the value is true, means the PDF is encrypted with password, otherwise it's not.

C#
using Spire.Pdf;
using System;

namespace PdfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "Sample.pdf";
            bool value = PdfDocument.IsPasswordProtected(fileName);

            Console.WriteLine(value);
            Console.ReadKey();

        }
    }
}
VB.NET
Imports Spire.Pdf
Imports System

Namespace PdfDemo
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim fileName As String = "Sample.pdf"
            Dim value As Boolean = PdfDocument.IsPasswordProtected(fileName)
            Console.WriteLine(value)
            Console.ReadKey
        End Sub
    End Class
End Namespace

After running the project, we get the Output that shows the PDF file is password protected:

Detect if the PDF file is password protected in C#