How to open password protected PDF?

Q:

I am trying to open a secured document using PDFNet. I have the
following code:

public void TestSecurity() {
try
{
  PDFNet.Initialize();
  string fileName = TestFilesDir + "File6_Encrypted.pdf";
  PDFDoc docTest = new PDFDoc(fileName);
  docTest.InitSecurityHandler();
  SecurityHandler securityHandler = docTest.GetSecurityHandler();
  bool hasReadPermission =
securityHandler.GetPermission(SecurityHandler.Permission.e_doc_open);
  PDFNet.Terminate();
  }
  catch(Exception ex) {
  }
}

When I executed securityHandler.GetPermission(), an "unknown exception"
was thrown (instead of return an expected bool value "false"). The file
I was processing was a password protected file, please see the
attachment, the password is "12345".
--------------

A:

If the file is protected with 'file open' password, you need to provide
the password as an argument to 'InitStdSecurityHandler()'. For example:

PDFNet.Initialize();

string fileName = TestFilesDir + "File6_Encrypted.pdf";
PDFDoc docTest = new PDFDoc(fileName);
docTest.InitStdSecurityHandler("12345"); // <-- password

SecurityHandler securityHandler = docTest.GetSecurityHandler();
bool hasReadPermission =
SecurityHandler.GetPermission(SecurityHandler.Permission.e_doc_open);
// PDFNet.Terminate(); usually not required

Another option is to derive a custom security handler (as illustrated
in EncTest sample project
(http://www.pdftron.com/net/samplecode.html#EncTest)) that will collect
any required authentication information via a callback interface.