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

Find Out Whether a PDF Needs a Password Before Loading It

See more PDF Protection Examples

Demonstrates PreLoadCheck, which examines a PDF file without loading it and without any password. It reports whether the file is encrypted, by which method and algorithm, whether a password is needed to open it, and what its permissions are. An application uses it to decide whether to ask for a password, or which certificate it needs, before calling LoadFile. PreLoadCheckBd does the same for a PDF held in memory.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vInfo
    Handle hoInfo
    String sTemp1
    Integer iTemp1
    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 two_page_memo.pdf

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

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

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

    //  A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoPdf "qa_data/pdf/two_page_memo.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "two_page_memo.pdf"
    Get ComBoolOf Of hoInfo "encrypted" To bTemp1
    Showln "  encrypted: " bTemp1

    //  A PDF that needs a password to open.
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "encrypted_user_password.pdf"
    Get ComBoolOf Of hoInfo "encrypted" To bTemp1
    Showln "  encrypted: " bTemp1
    Get ComStringOf Of hoInfo "method" To sTemp1
    Showln "  method: " sTemp1
    Get ComStringOf Of hoInfo "algorithm" To sTemp1
    Showln "  algorithm: " sTemp1
    Get ComBoolOf Of hoInfo "openPasswordRequired" To bTemp1
    Showln "  openPasswordRequired: " bTemp1

    //  A PDF that opens without a password but is restricted.  The permissions are readable without
    //  any password.
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoPdf "qa_data/pdf/no_copy_permission.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "no_copy_permission.pdf"
    Get ComBoolOf Of hoInfo "encrypted" To bTemp1
    Showln "  encrypted: " bTemp1
    Get ComBoolOf Of hoInfo "openPasswordRequired" To bTemp1
    Showln "  openPasswordRequired: " bTemp1
    Get ComBoolOf Of hoInfo "permissions.copy" To bTemp1
    Showln "  may copy: " bTemp1

    //  The typical use: ask for a password only when one is needed, then load.
    Get pvComObject of hoInfo to vInfo
    Get ComPreLoadCheck Of hoPdf "qa_data/pdf/encrypted_user_password.pdf" vInfo To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoInfo "openPasswordRequired" To bTemp1
    If (bTemp1 = True) Begin
        //  An application would prompt for the password here.
        Set ComUserPassword Of hoPdf To "secret"
    End

    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 ComNumPages Of hoPdf To iTemp1
    Showln "loaded, pages: " iTemp1

    //  Sample output:

    //  two_page_memo.pdf
    //    encrypted: False
    //  encrypted_user_password.pdf
    //    encrypted: True
    //    method: password
    //    algorithm: rc4-128
    //    openPasswordRequired: True
    //  no_copy_permission.pdf
    //    encrypted: True
    //    openPasswordRequired: False
    //    may copy: False
    //  loaded, pages: 1


End_Procedure