How to determine the password options for a PDF?

Question:
How do I determine if a PDF requires a password, and also what password was entered?

Answer:
The following C# code shows how to accomplish this.

// let p be either null or password
var doc = new PDFDoc(f);
bool userPasswordReq = !doc.InitSecurityHandler();
if(userPasswordReq || p != null)
{
    doc.InitStdSecurityHandler(p);
}
bool isEncrypted = doc.IsEncrypted();
if(isEncrypted)
{
    var security = doc.GetSecurityHandler();
    var stdSec = new StdSecurityHandler(security);
    var userReq = stdSec.IsUserPasswordRequired(); // contains user password
    var ownerReq = stdSec.IsMasterPasswordRequired(); // contains owner password
    var ownerRights = security.GetPermission(SecurityHandler.Permission.e_owner); // current reader has owner rights

    // Console.WriteLine($"\tisEncrypted: {isEncrypted}");
    // Console.WriteLine($"\tpasswordReq: {userPasswordReq}");
    // Console.WriteLine($"\t    userReq: {userReq}");
    // Console.WriteLine($"\t   ownerReq: {ownerReq}");
    // Console.WriteLine($"\townerRights: {ownerRights}");
}

The following is the same for NodeJS

// let p be either null or password
const doc = await PDFNet.PDFDoc.createFromFilePath(f);
const userPasswordReq = !await doc.initSecurityHandler();
if(userPasswordReq || p)
{
  await doc.initStdSecurityHandler(p);
}
const isEncrypted = await doc.isEncrypted();
if(isEncrypted)
{
  const security = await doc.getSecurityHandler();
  const userReq = await security.isUserPasswordRequired(); // encryption contains user password
  const ownerReq = await security.isMasterPasswordRequired(); // encryption contains owner password
  const ownerRights = await security.getPermission(PDFNet.SecurityHandler.Permission.e_owner); // current reader has owner rights

  // console.log(f + " with password " + p);
  // console.log("\tpasswordReq: " + userPasswordReq);
  // console.log("\t    userReq: " + userReq);
  // console.log("\t   ownerReq: " + ownerReq);
  // console.log("\townerRights: " + ownerRights);
}