Need a function that can determine if a PDF is password-protected

Q:

We need a function that can determine if a PDF is password-protected.

  1. Do you have a function that can just tell us if a PDF has password protection set?
  2. Do you have a function that can determine if the metadata packet is encrypted or not?

A:

PDFDoc.InitSecurityHandler() returns false if you need a password to open the document ( document is protected using so called ‘open’ password).
https://www.pdftron.com/api/PDFNet/html/M_pdftron_PDF_PDFDoc_InitSecurityHandler.htm

A couple of asks:

  1. Do you have a function that can just tell us if a PDF has password protection set?

Yes, you can use InitSecurityHandler() to determine if a PDF requires an ‘open’ password.
If InitSecurityHandler() returns true, it means you do not need a password to open a document ( keep in mind that the file may still be encrypted, but this is weak encryption and it can be easily removed ).

  1. Do you have a function that can determine if the metadata packet is encrypted or not?

Yes, you can find out whether metadata packet is encrypted or not as follows:

Obj o = doc.GetTrailer().FindObj(“Encrypt”);
bool metadata_encrypted = false;
if (o != null) {
if (o.FindObj(“EncryptMetadata”) != null)
metadata_encrypted = o.FindObj(“EncryptMetadata”).GetBool();
}

This is C# pseudocode, however the same API works in all languages/platforms.

Do we have any such function for the Doc, Docx and other Office formats to determine if the file is password-protected or not?

Can anyone suggest or help.