Tcl Requires Chilkat v11.7.0+
Tcl
A PDF Watermark That Only Appears When Printed
See more PDF Watermark Examples
Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.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
}
set json [new_CkJsonObject]
CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "UNCONTROLLED WHEN PRINTED"
CkJsonObject_UpdateString $json "id" "printonly"
# As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
CkJsonObject_UpdateString $json "approach" "layer"
CkJsonObject_UpdateString $json "layer.name" "Print marking"
CkJsonObject_UpdateString $json "layer.view" "off"
CkJsonObject_UpdateString $json "layer.print" "on"
CkJsonObject_UpdateBool $json "layer.locked" 1
CkJsonObject_UpdateString $json "rotation" "diagonal"
CkJsonObject_UpdateString $json "scale.fit" "page"
CkJsonObject_UpdateNumber $json "scale.fitPercent" "70"
CkJsonObject_UpdateNumber $json "opacity" "0.3"
set jsonOut [new_CkJsonObject]
set success [CkPdf_Watermark $pdf "add" $json $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
puts "layer id: [CkJsonObject_stringOf $jsonOut id] pages: [CkJsonObject_stringOf $jsonOut pages]"
set success [CkPdf_WriteFile $pdf "qa_output/memo_print_only.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
exit
}
# The same thing as an annotation instead.
set pdf2 [new_CkPdf]
set success [CkPdf_LoadFile $pdf2 "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf2]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $pdf2
exit
}
set json2 [new_CkJsonObject]
CkJsonObject_UpdateString $json2 "type" "text"
CkJsonObject_UpdateString $json2 "text" "UNCONTROLLED WHEN PRINTED"
CkJsonObject_UpdateString $json2 "id" "printonly2"
CkJsonObject_UpdateString $json2 "approach" "annotation"
CkJsonObject_UpdateBool $json2 "annotation.view" 0
CkJsonObject_UpdateBool $json2 "annotation.print" 1
CkJsonObject_UpdateBool $json2 "annotation.fixedPrint" 1
CkJsonObject_UpdateString $json2 "rotation" "diagonal"
set success [CkPdf_Watermark $pdf2 "add" $json2 $jsonOut]
if {$success == 0} then {
puts [CkPdf_lastErrorText $pdf2]
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $pdf2
delete_CkJsonObject $json2
exit
}
puts "annotation id: [CkJsonObject_stringOf $jsonOut id]"
# Sample output:
# layer id: printonly pages: 1-2
# annotation id: printonly2
delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $pdf2
delete_CkJsonObject $json2