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

Tcl

load ./chilkat.dll

set success 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

set pdf [new_CkPdf]

#  Load with the current passwords.
CkPdf_put_UserPassword $pdf "secret"
CkPdf_put_OwnerPassword $pdf "owner-secret"
set success [CkPdf_LoadFile $pdf "qa_data/pdf/encrypted_user_password.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

#  The new passwords.  Protection reads the properties as they are when it is called.
CkPdf_put_UserPassword $pdf "new-user-secret"
CkPdf_put_OwnerPassword $pdf "new-owner-secret"

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

set success [CkPdf_Protection $pdf "changePasswords" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  The algorithm and the permissions are kept.
puts "algorithm: [CkJsonObject_stringOf $jsonOut pending.algorithm]"
puts "password needed to open: [CkJsonObject_BoolOf $jsonOut pending.userPasswordRequired]"

set success [CkPdf_WriteFile $pdf "qa_output/minutes_new_passwords.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    exit
}

#  The new user password opens the result.
set reader [new_CkPdf]

CkPdf_put_UserPassword $reader "new-user-secret"
set success [CkPdf_LoadFile $reader "qa_output/minutes_new_passwords.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $reader]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    exit
}

set success [CkPdf_Protection $reader "getInfo" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $reader]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    exit
}

puts "with the new user password, opened with: [CkJsonObject_stringOf $jsonOut loaded.openedWith]"

#  And so does the new owner password.
set owner [new_CkPdf]

CkPdf_put_OwnerPassword $owner "new-owner-secret"
set success [CkPdf_LoadFile $owner "qa_output/minutes_new_passwords.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    delete_CkPdf $owner
    exit
}

set success [CkPdf_Protection $owner "getInfo" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $reader
    delete_CkPdf $owner
    exit
}

puts "with the new owner password, opened with: [CkJsonObject_stringOf $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

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkPdf $owner