Tcl Requires Chilkat v11.7.0+
Tcl
Optimizing an Encrypted PDF
See more PDF Optimization Examples
Demonstrates optimizing a PDF that is encrypted. Set the password before loading, exactly as for any other operation; the optimized PDF keeps the security it had, so the output still needs the same password. Because Chilkat honours the permissions recorded in the document, optimizing requires owner rights or the "modify" permission: a PDF opened with a user password that forbids modification can be audited but not optimized.Chilkat Tcl Downloads
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]
# The password must be set before loading.
CkPdf_put_UserPassword $pdf "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
}
# What security does the file have, and may it be modified?
set json [new_CkJsonObject]
set jsonOut [new_CkJsonObject]
set success [CkPdf_Protection $pdf "getInfo" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
puts "encrypted: [CkJsonObject_BoolOf $jsonOut loaded.encrypted]"
puts "method: [CkJsonObject_stringOf $jsonOut loaded.method]"
puts "openedWith: [CkJsonObject_stringOf $jsonOut loaded.openedWith]"
puts "may modify: [CkJsonObject_BoolOf $jsonOut loaded.permissions.modify]"
set success [CkPdf_Optimize $pdf "optimize" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
set success [CkPdf_WriteFile $pdf "qa_output/encrypted_optimized.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
puts "bytesSaved: [CkJsonObject_IntOf $jsonOut bytesSaved]"
# The output is still encrypted with the same security: loading it without the password fails.
set noPassword [new_CkPdf]
set success [CkPdf_LoadFile $noPassword "qa_output/encrypted_optimized.pdf"]
set q [new_CkJsonObject]
set qOut [new_CkJsonObject]
set success [CkPdf_ExtractText $noPassword "assess" $q $qOut]
puts "passwordRequired without the password: [CkJsonObject_BoolOf $qOut quality.passwordRequired]"
# With the password it reads normally.
set withPassword [new_CkPdf]
CkPdf_put_UserPassword $withPassword "secret"
set success [CkPdf_LoadFile $withPassword "qa_output/encrypted_optimized.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $withPassword]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $noPassword
delete_CkJsonObject $q
delete_CkJsonObject $qOut
delete_CkPdf $withPassword
exit
}
puts "pages: [CkPdf_get_NumPages $withPassword]"
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $noPassword
delete_CkJsonObject $q
delete_CkJsonObject $qOut
delete_CkPdf $withPassword