Check if PDF document is password protected

Docotic.Pdf Library Help > Samples > General operations > Check if PDF document is password protected
Docotic.Pdf documentation

This sample shows how to check if an existing PDF document is password protected.

You can check if a PDF document is password protected using PdfDocument.IsPasswordProtected method. This method is static, so you can perform a check before opening a PDF document. This is useful if you don't know beforehand if a password is required to open a PDF document.

C# 
using System.Text;
using System.Windows.Forms;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class CheckIfPasswordProtected
    {
        public static void Main()
        {
            StringBuilder message = new StringBuilder();

            string[] documentsToCheck = { "encrypted.pdf", "jfif3.pdf" };
            foreach (string fileName in documentsToCheck)
            {
                bool passwordProtected = PdfDocument.IsPasswordProtected(@"Sample Data\" + fileName);
                if (passwordProtected)
                    message.AppendFormat("{0} - REQUIRES PASSWORD\r\n", fileName);
                else
                    message.AppendFormat("{0} - DOESN'T REQUIRE PASSWORD\r\n", fileName);
            }

            MessageBox.Show(message.ToString());
        }
    }
}
Visual Basic 
Imports System.Text
Imports System.Windows.Forms

Imports Microsoft.VisualBasic

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class CheckIfPasswordProtected
        Public Shared Sub Main()
            Dim message As New StringBuilder()

            Dim documentsToCheck As String() = {"encrypted.pdf", "jfif3.pdf"}
            For Each fileName As String In documentsToCheck
                Dim passwordProtected As Boolean = PdfDocument.IsPasswordProtected("Sample Data\" + fileName)
                If passwordProtected Then
                    message.AppendFormat("{0} - REQUIRES PASSWORD" & vbCr & vbLf, fileName)
                Else
                    message.AppendFormat("{0} - DOESN'T REQUIRE PASSWORD" & vbCr & vbLf, fileName)
                End If
            Next

            MessageBox.Show(message.ToString())
        End Sub
    End Class
End Namespace