Tcl Requires Chilkat v11.7.0+
Tcl
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.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 two_page_memo.pdf
set pdf [new_CkPdf]
set success [CkPdf_LoadFile $pdf "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
exit
}
# No user password: the PDF opens without a prompt. The owner password is what protects the
# restrictions, so it must not be empty when anything is restricted.
CkPdf_put_UserPassword $pdf ""
CkPdf_put_OwnerPassword $pdf "owner-secret"
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "permissions.print" "lowQuality"
CkJsonObject_UpdateBool $json "permissions.copy" 0
CkJsonObject_UpdateBool $json "permissions.modify" 0
CkJsonObject_UpdateBool $json "permissions.annotate" 0
# The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
set jsonOut [new_CkJsonObject]
set success [CkPdf_Protection $pdf "encrypt" $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/memo_restricted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# Anyone can open the result, with the rights of a user.
set reader [new_CkPdf]
set success [CkPdf_LoadFile $reader "qa_output/memo_restricted.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 "opened with: [CkJsonObject_stringOf $info loaded.openedWith]"
puts "print: [CkJsonObject_stringOf $info loaded.permissions.print]"
puts "copy: [CkJsonObject_BoolOf $info loaded.permissions.copy]"
puts "modify: [CkJsonObject_BoolOf $info loaded.permissions.modify]"
# Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
set opts [new_CkJsonObject]
set extractOut [new_CkJsonObject]
set sb [new_CkStringBuilder]
set success [CkPdf_ExtractTextSb $reader "text" $opts $sb $extractOut]
if {$success == 0} then {
puts "Text extraction was refused: the PDF does not permit copying."
}
# The owner password lifts every restriction.
set owner [new_CkPdf]
CkPdf_put_OwnerPassword $owner "owner-secret"
set success [CkPdf_LoadFile $owner "qa_output/memo_restricted.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $owner]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $empty
delete_CkJsonObject $info
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb
delete_CkPdf $owner
exit
}
set ownerInfo [new_CkJsonObject]
set success [CkPdf_Protection $owner "getInfo" $empty $ownerInfo]
if {$success == 0} then {
puts [CkPdf_lastErrorText $owner]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $empty
delete_CkJsonObject $info
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb
delete_CkPdf $owner
delete_CkJsonObject $ownerInfo
exit
}
puts "opened with: [CkJsonObject_stringOf $ownerInfo loaded.openedWith]"
set success [CkPdf_ExtractTextSb $owner "text" $opts $sb $extractOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $owner]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $empty
delete_CkJsonObject $info
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb
delete_CkPdf $owner
delete_CkJsonObject $ownerInfo
exit
}
puts "characters extracted with the owner password: [CkStringBuilder_get_Length $sb]"
# Sample output:
# opened with: emptyPassword
# print: lowQuality
# copy: False
# modify: False
# Text extraction was refused: the PDF does not permit copying.
# opened with: owner
# characters extracted with the owner password: 1048
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $reader
delete_CkJsonObject $empty
delete_CkJsonObject $info
delete_CkJsonObject $opts
delete_CkJsonObject $extractOut
delete_CkStringBuilder $sb
delete_CkPdf $owner
delete_CkJsonObject $ownerInfo