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

PDF Owner Password vs. User Password

See more PDF Protection Examples

Demonstrates the difference between the two passwords of an encrypted PDF. Either one opens the file. The user password gives the rights the permissions grant. The owner password gives owner rights: no restriction applies, and it is the only one that lets the security be changed or removed. Every Protection operation except getInfo needs owner rights, and Chilkat does not offer a way around that.

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 json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  Opened with the user password.
set user [new_CkPdf]

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

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

puts "opened with: [CkJsonObject_stringOf $jsonOut loaded.openedWith]"

#  Changing the security needs owner rights, so this is refused.
set success [CkPdf_Protection $user "removeEncryption" $json $jsonOut]
if {$success == 0} then {
    puts "removeEncryption was refused: the PDF was not opened with its owner password."
}

#  Opened with the owner password.
set owner [new_CkPdf]

CkPdf_put_OwnerPassword $owner "owner-secret"
set success [CkPdf_LoadFile $owner "qa_data/pdf/encrypted_user_password.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $user
    delete_CkPdf $owner
    exit
}

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

puts "opened with: [CkJsonObject_stringOf $jsonOut loaded.openedWith]"

#  Now it is allowed.
set success [CkPdf_Protection $owner "removeEncryption" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $owner]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $user
    delete_CkPdf $owner
    exit
}

puts "removeEncryption succeeded; will be saved encrypted: [CkJsonObject_BoolOf $jsonOut pending.encrypted]"

#  Sample output:

#  opened with: user
#  removeEncryption was refused: the PDF was not opened with its owner password.
#  opened with: owner
#  removeEncryption succeeded; will be saved encrypted: False

delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $user
delete_CkPdf $owner