Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.7.0+

Get the Encryption and Permissions of a Loaded PDF

See more PDF Protection Examples

Demonstrates the getInfo operation of Protection, which reports how the loaded PDF is protected: the method and algorithm, whether a password is needed to open it, how it was opened (with the user password, the owner password, an empty password, or a certificate), and each permission. The loaded member describes the file as it is, the pending member describes the security it will be saved with, and changed says whether the two differ. getInfo is the one operation that does not need owner rights.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  The PDF used in this example is available at encrypted_user_password.pdf

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End

    //  The password must be set before loading.
    Set ComUserPassword Of hoPdf To "secret"
    Get ComLoadFile Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComProtection Of hoPdf "getInfo" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComEmitCompact Of hoJsonOut To False
    Get ComEmit Of hoJsonOut To sTemp1
    Showln sTemp1

    //  "loaded" is the security the file has.  "pending" is the security it will be saved with, which
    //  is the same until an operation such as encrypt or changePermissions changes it.
    Get ComStringOf Of hoJsonOut "loaded.algorithm" To sTemp1
    Showln "algorithm: " sTemp1
    Get ComStringOf Of hoJsonOut "loaded.openedWith" To sTemp1
    Showln "opened with: " sTemp1
    Get ComBoolOf Of hoJsonOut "loaded.userPasswordRequired" To bTemp1
    Showln "password needed to open: " bTemp1
    Get ComStringOf Of hoJsonOut "loaded.permissions.print" To sTemp1
    Showln "may print: " sTemp1
    Get ComBoolOf Of hoJsonOut "loaded.permissions.copy" To bTemp1
    Showln "may copy: " bTemp1
    Get ComBoolOf Of hoJsonOut "changed" To bTemp1
    Showln "changed: " bTemp1

    //  Sample output:

    //  {
    //    "changed": false,
    //    "loaded": {
    //      "encrypted": true,
    //      "method": "password",
    //      "algorithm": "rc4-128",
    //      "pdfVersion": "1.3",
    //      "scope": "all",
    //      "userPasswordRequired": true,
    //      "ownerPasswordSet": true,
    //      "openedWith": "user",
    //      "permissions": {
    //        "print": "full",
    //        "modify": true,
    //        "copy": true,
    //        "accessibility": true,
    //        "annotate": true,
    //        "fillForms": true,
    //        "assemble": true
    //      }
    //    },
    //    "pending": {
    //      "encrypted": true,
    //      "method": "password",
    //      "algorithm": "rc4-128",
    //      "pdfVersion": "1.3",
    //      "scope": "all",
    //      "userPasswordRequired": true,
    //      "ownerPasswordSet": true,
    //      "permissions": {
    //        "print": "full",
    //        "modify": true,
    //        "copy": true,
    //        "accessibility": true,
    //        "annotate": true,
    //        "fillForms": true,
    //        "assemble": true
    //      }
    //    }
    //  }
    //  
    //  algorithm: rc4-128
    //  opened with: user
    //  password needed to open: True
    //  may print: full
    //  may copy: True
    //  changed: False


End_Procedure