Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 920
lance Avatar asked Aug 08 '11 16:08

lance


2 Answers

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

like image 59
Yahia Avatar answered Nov 07 '22 12:11

Yahia


Docotic.Pdf library can be used to perform such check.

The library provides group of methods that can be used to check if bytes, stream or file contain a password protected PDF document.

Please take a look at the sample that shows how to check if an existing PDF document is password protected.

Disclaimer: I work for the company.

like image 45
Bobrovsky Avatar answered Nov 07 '22 10:11

Bobrovsky