9

I want to check if a pdf file is password protected or not to view. That is i want to know if the pdf file has user password or not.

I found some help in some forum about it to use isencrypted function but it does not give correct answer.

Is it possible to check if a pdf is password protected?

Md Kamruzzaman Sarker
  • 2,387
  • 3
  • 22
  • 38
  • `I found some help in some forum about it to use isencrypted function but it does not give correct answer` -- That doesn't sound encouraging. – Robert Harvey Jul 02 '12 at 17:53

4 Answers4

17

The problem with using the PdfReader.IsEncrypted method is that if you attempt to instantiate a PdfReader on a PDF that requires a password - and you don't supply that password - you'll get a BadPasswordException.

Keeping this in mind you can write a method like this:

public static bool IsPasswordProtected(string pdfFullname) {
    try {
        PdfReader pdfReader = new PdfReader(pdfFullname);
        return false;
    } catch (BadPasswordException) {
        return true;
    }
}

Note that if you supply an invalid password you'll get the same BadPasswordException when attempting to construct a PdfReader object. You can use this to create a method that validates a PDF's password:

public static bool IsPasswordValid(string pdfFullname, byte[] password) {
    try {
        PdfReader pdfReader = new PdfReader(pdfFullname, password);
        return false;
    } catch (BadPasswordException) {
        return true;
    }
}

Sure it's ugly but as far as I know this is the only way to check if a PDF is password protected. Hopefully someone will suggest a better solution.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • Nice approach, useful to me, I just made a little change on your code, you had true and false returns on IsPasswordValid resulting in non intuitive answer for when password was valid, it was returning false and vice versa! – Abe Jan 14 '16 at 00:12
  • 1
    You should update the example to include a call to Dispose() for the PdfReader object. – NickC Mar 22 '16 at 12:35
  • 2
    seems that the error is no longer thrown on open and waits for you to take an action. there is a full access property that can be checked to figure out if there is a password that was not provided – workabyte Mar 28 '19 at 14:20
7
  private void CheckPdfProtection(string filePath)
        {
            try
            {
                PdfReader reader = new PdfReader(filePath);
                if (!reader.IsEncrypted()) return;
                if (!PdfEncryptor.IsPrintingAllowed(reader.Permissions))
                    throw new InvalidOperationException("the selected file is print protected and cannot be imported");
                if (!PdfEncryptor.IsModifyContentsAllowed(reader.Permissions))
                    throw new InvalidOperationException("the selected file is write protected and cannot be imported");
            }
            catch (BadPasswordException) { throw new InvalidOperationException("the selected file is password protected and cannot be imported"); }
            catch (BadPdfFormatException) { throw new InvalidDataException("the selected file is having invalid format and cannot be imported"); }
        }
user3232077
  • 71
  • 1
  • 1
2

Reference : Check for Full Permission

You should be able to just check the property PdfReader.IsOpenedWithFullPermissions.

PdfReader r = new PdfReader("YourFile.pdf");
if (r.IsOpenedWithFullPermissions)
{
    //Do something
}
Community
  • 1
  • 1
Ebad Masood
  • 2,389
  • 28
  • 46
  • No. It gives exception in this line PdfReader r = new PdfReader("YourFile.pdf"); for password protected file. just check a password protected pdf file with this code. – Md Kamruzzaman Sarker Jul 02 '12 at 18:23
  • check this with password protected file.... you can see the exception try { PdfReader r = new PdfReader("YourFile.pdf"); if (r.IsOpenedWithFullPermissions) { //Do something } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } – Md Kamruzzaman Sarker Jul 02 '12 at 18:30
  • 2
    @MdKamruzzamanPallob: can you add the exception to your question, please? (It's non-trivial for someone else to set this up, so they are unlikely to do so - be detailed and helpful with your answers when seeking help!) – halfer Jul 02 '12 at 21:15
-1

Just in case it ends up helping someone, here's a simple solution I have been using in vb.net. The problem with checking with fullpermissions (As mentioned above) is that you can't actually open a PDF that has a password that prevents you from opening it. I also have something I do about check for that in the code below. itextsharp.text.pdf has a few exceptions you might find useful actually, check it out if this isn't doing what you need.

Dim PDFDoc As PdfReader
Try
    PDFDoc = New PdfReader(PDFToCheck)
If PDFDoc.IsOpenedWithFullPermissions = False Then
   'PDF prevents things but it can still be opened. e.g. printing.
end if
Catch ex As iTextSharp.text.pdf.BadPasswordException
    'this exception means the PDF can't be opened at all. 
Finally 
    'do whatever if things are normal!
End Try
Finch042
  • 307
  • 3
  • 9
  • 18