Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  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

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    info.i = CkJsonObject::ckCreate()
    If info.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  A PDF that is not encrypted.  Nothing but "encrypted" is reported for it.
    success = CkPdf::ckPreLoadCheck(pdf,"qa_data/pdf/two_page_memo.pdf",info)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "two_page_memo.pdf"
    Debug "  encrypted: " + Str(CkJsonObject::ckBoolOf(info,"encrypted"))

    ;  A PDF that needs a password to open.
    success = CkPdf::ckPreLoadCheck(pdf,"qa_data/pdf/encrypted_user_password.pdf",info)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "encrypted_user_password.pdf"
    Debug "  encrypted: " + Str(CkJsonObject::ckBoolOf(info,"encrypted"))
    Debug "  method: " + CkJsonObject::ckStringOf(info,"method")
    Debug "  algorithm: " + CkJsonObject::ckStringOf(info,"algorithm")
    Debug "  openPasswordRequired: " + Str(CkJsonObject::ckBoolOf(info,"openPasswordRequired"))

    ;  A PDF that opens without a password but is restricted.  The permissions are readable without
    ;  any password.
    success = CkPdf::ckPreLoadCheck(pdf,"qa_data/pdf/no_copy_permission.pdf",info)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "no_copy_permission.pdf"
    Debug "  encrypted: " + Str(CkJsonObject::ckBoolOf(info,"encrypted"))
    Debug "  openPasswordRequired: " + Str(CkJsonObject::ckBoolOf(info,"openPasswordRequired"))
    Debug "  may copy: " + Str(CkJsonObject::ckBoolOf(info,"permissions.copy"))

    ;  The typical use: ask for a password only when one is needed, then load.
    success = CkPdf::ckPreLoadCheck(pdf,"qa_data/pdf/encrypted_user_password.pdf",info)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckBoolOf(info,"openPasswordRequired") = 1
        ;  An application would prompt for the password here.
        CkPdf::setCkUserPassword(pdf, "secret")
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(info)
        ProcedureReturn
    EndIf

    Debug "loaded, pages: " + Str(CkPdf::ckNumPages(pdf))

    ;  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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(info)


    ProcedureReturn
EndProcedure