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

Change the Passwords of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePasswords operation, which gives an encrypted PDF new user and owner passwords and keeps its algorithm and permissions. The PDF is loaded with its current passwords, the two properties are then set to the new ones, and Protection reads them at the time of the call. Both passwords are replaced, so set both: a property left empty becomes an empty password.

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 encrypted_user_password.pdf

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

    ;  Load with the current passwords.
    CkPdf::setCkUserPassword(pdf, "secret")
    CkPdf::setCkOwnerPassword(pdf, "owner-secret")
    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/encrypted_user_password.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    ;  The new passwords.  Protection reads the properties as they are when it is called.
    CkPdf::setCkUserPassword(pdf, "new-user-secret")
    CkPdf::setCkOwnerPassword(pdf, "new-owner-secret")

    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

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

    ;  The algorithm and the permissions are kept.
    Debug "algorithm: " + CkJsonObject::ckStringOf(jsonOut,"pending.algorithm")
    Debug "password needed to open: " + Str(CkJsonObject::ckBoolOf(jsonOut,"pending.userPasswordRequired"))

    success = CkPdf::ckWriteFile(pdf,"qa_output/minutes_new_passwords.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ;  The new user password opens the result.
    reader.i = CkPdf::ckCreate()
    If reader.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkPdf::setCkUserPassword(reader, "new-user-secret")
    success = CkPdf::ckLoadFile(reader,"qa_output/minutes_new_passwords.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(reader)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        ProcedureReturn
    EndIf

    success = CkPdf::ckProtection(reader,"getInfo",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(reader)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        ProcedureReturn
    EndIf

    Debug "with the new user password, opened with: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")

    ;  And so does the new owner password.
    owner.i = CkPdf::ckCreate()
    If owner.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkPdf::setCkOwnerPassword(owner, "new-owner-secret")
    success = CkPdf::ckLoadFile(owner,"qa_output/minutes_new_passwords.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(owner)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        CkPdf::ckDispose(owner)
        ProcedureReturn
    EndIf

    success = CkPdf::ckProtection(owner,"getInfo",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(owner)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkPdf::ckDispose(reader)
        CkPdf::ckDispose(owner)
        ProcedureReturn
    EndIf

    Debug "with the new owner password, opened with: " + CkJsonObject::ckStringOf(jsonOut,"loaded.openedWith")

    ;  Sample output:

    ;  algorithm: rc4-128
    ;  password needed to open: True
    ;  with the new user password, opened with: user
    ;  with the new owner password, opened with: owner


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


    ProcedureReturn
EndProcedure