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

Extract Text from a Password-Protected PDF

See more PDF Text Extraction Examples

Demonstrates extracting text from a PDF that requires a password to open. The PDF loads without the password (its structure can be read), but the "text" operation fails because the content cannot be decrypted; "assess" reports that a password is required. With the UserPassword property set before loading, extraction works normally.

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 (user password: secret) is available at encrypted_user_password.pdf

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

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

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

    ;  The PDF loads without its password: the file's structure is readable, so properties such as
    ;  NumPages and the security settings are available...
    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  ...but the content cannot be decrypted, so extracting text fails.
    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug "Cannot extract: " + CkPdf::ckLastErrorText(pdf)
    EndIf

    ;  "assess" still works: it reports that a password is required and nothing else.
    success = CkPdf::ckExtractText(pdf,"assess",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "passwordRequired: " + Str(CkJsonObject::ckBoolOf(jsonOut,"quality.passwordRequired"))
    Debug "rating: " + CkJsonObject::ckStringOf(jsonOut,"quality.rating")

    ;  Set the password and load again.
    CkPdf::setCkUserPassword(pdf, "secret")
    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug CkJsonObject::ckStringOf(jsonOut,"text")

    ;  Sample output:

    ;  Cannot extract: ChilkatLog:
    ;    ExtractText:
    ;      ...
    ;        The PDF is encrypted and could not be opened (wrong or missing password); its text cannot be read.
    ;      ...
    ;  passwordRequired: true
    ;  rating: none
    ;  Board Minutes (Confidential)
    ;  
    ;  The board met on 3 November 2026. Present: the chair, the treasurer and four members. The treasurer reported that spending for the season came in under budget.
    ;  
    ;  The meeting closed at 8.45 p.m.


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure