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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

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 four_pages.pdf

set pdf [new_CkPdf]

set success [CkPdf_LoadFile $pdf "qa_data/pdf/four_pages.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    exit
}

puts "pages in the document: [CkPdf_get_NumPages $pdf]"

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "INTERNAL"
CkJsonObject_UpdateString $json "id" "internal"

#  Every page except the first.
CkJsonObject_UpdateString $json "pages" "all"
CkJsonObject_UpdateString $json "excludePages" "1"
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 "every page but the cover: [CkJsonObject_stringOf $jsonOut pages]"

#  A list of pages and ranges, on a second copy of the document.
set pdf2 [new_CkPdf]

set success [CkPdf_LoadFile $pdf2 "qa_data/pdf/four_pages.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" "APPENDIX"
CkJsonObject_UpdateString $json2 "id" "appendix"
CkJsonObject_UpdateString $json2 "pages" "2,4-"
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 "page 2 and 4 onwards: [CkJsonObject_stringOf $jsonOut pages]"

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

#  Sample output:

#  pages in the document: 4
#  every page but the cover: 2-4
#  page 2 and 4 onwards: 2,4

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $pdf2
delete_CkJsonObject $json2