Search code examples
c#pdfdetectionpassword-protection

Is this byte array a password-protected PDF document?


I have a C# byte array, and I need to know if it's a password-protected PDF document. How can I make this determination?

I'm inspecting the document for the PDF magic cookie to determine if it's a PDF document, but I don't know what's best when determining if it's password protected.

I don't need to open the document (though, as long as there's no UI required -- this is a background process, I'm OK with doing that if it helps).

I just need to know if there's a password on this PDF document.

EDIT: If you cite iTextSharp, I'd be very grateful if you'd cite a specific resource within that API that addresses this concern.


Solution

  • There are several possibilities for password-protecting PDF, some defined by Adobe ("standard") and as part of the standard it is possible to have so-called "alternate security handlers".

    Anyway all can be detected basically by checking for the respective "dictionaries" as described on pages 115 - 136 of the PDF spec - see http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf