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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

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]

#  Changing the permissions needs owner rights.  Both passwords are given here, which is what
#  an AES-256 PDF requires for this operation.
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
}

#  No printing and no copying.  Everything not mentioned is granted.
set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "permissions.print" "none"
CkJsonObject_UpdateBool $json "permissions.copy" 0

set jsonOut [new_CkJsonObject]

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

puts "print: [CkJsonObject_stringOf $jsonOut pending.permissions.print]"
puts "copy: [CkJsonObject_BoolOf $jsonOut pending.permissions.copy]"
puts "modify: [CkJsonObject_BoolOf $jsonOut pending.permissions.modify]"
puts "password still needed to open: [CkJsonObject_BoolOf $jsonOut pending.userPasswordRequired]"

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

#  The same user password opens the result, now with the new permissions.
set reader [new_CkPdf]

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

set empty [new_CkJsonObject]

set info [new_CkJsonObject]

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

puts "saved file, print: [CkJsonObject_stringOf $info loaded.permissions.print]"
puts "saved file, copy: [CkJsonObject_BoolOf $info loaded.permissions.copy]"

#  Sample output:

#  print: none
#  copy: False
#  modify: True
#  password still needed to open: True
#  saved file, print: none
#  saved file, copy: False

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $empty
delete_CkJsonObject $info