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

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oReader
LOCAL oOwner

nSuccess := 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

oPdf := CreateObject("Chilkat.Pdf")

//  Load with the current passwords.
oPdf:UserPassword := "secret"
oPdf:OwnerPassword := "owner-secret"
nSuccess := oPdf:LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

//  The new passwords.  Protection reads the properties as they are when it is called.
oPdf:UserPassword := "new-user-secret"
oPdf:OwnerPassword := "new-owner-secret"

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("changePasswords", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  The algorithm and the permissions are kept.
? "algorithm: " + oJsonOut:StringOf("pending.algorithm")
? "password needed to open: " + Str(oJsonOut:BoolOf("pending.userPasswordRequired"))

nSuccess := oPdf:WriteFile("qa_output/minutes_new_passwords.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  The new user password opens the result.
oReader := CreateObject("Chilkat.Pdf")
oReader:UserPassword := "new-user-secret"
nSuccess := oReader:LoadFile("qa_output/minutes_new_passwords.pdf")
IF (nSuccess == 0)
    ? oReader:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    RETURN
ENDIF

nSuccess := oReader:Protection("getInfo", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oReader:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    RETURN
ENDIF

? "with the new user password, opened with: " + oJsonOut:StringOf("loaded.openedWith")

//  And so does the new owner password.
oOwner := CreateObject("Chilkat.Pdf")
oOwner:OwnerPassword := "new-owner-secret"
nSuccess := oOwner:LoadFile("qa_output/minutes_new_passwords.pdf")
IF (nSuccess == 0)
    ? oOwner:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    oOwner:destroy()
    RETURN
ENDIF

nSuccess := oOwner:Protection("getInfo", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oOwner:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    oOwner:destroy()
    RETURN
ENDIF

? "with the new owner password, opened with: " + oJsonOut:StringOf("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

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oOwner:destroy()