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

2021-02-07 03:47:35 Written by  support iceblue
Rate this item
(0 votes)

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#

Additional Info

  • tutorial_title:
Last modified on Sunday, 26 September 2021 01:40