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

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

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
}

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}"
CkJsonObject_UpdateString $json "id" "pagefoot"
CkJsonObject_UpdateString $json "position.align" "bottom"
CkJsonObject_UpdateNumber $json "position.margin" "24"
CkJsonObject_UpdateNumber $json "font.size" "9"
CkJsonObject_UpdateString $json "color" "#505050"

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 "footer pages: [CkJsonObject_stringOf $jsonOut pages]"

#  Roman numerals that start at i on page 2, so that a cover page is not numbered.
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" "- {page} -"
CkJsonObject_UpdateString $json2 "id" "roman"
CkJsonObject_UpdateString $json2 "pages" "2-"
CkJsonObject_UpdateInt $json2 "pageNumber.start" 1
CkJsonObject_UpdateString $json2 "pageNumber.style" "romanLower"
CkJsonObject_UpdateString $json2 "position.align" "bottom"
CkJsonObject_UpdateNumber $json2 "font.size" "10"
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 "roman pages: [CkJsonObject_stringOf $jsonOut pages]"

set success [CkPdf_WriteFile $pdf "qa_output/four_pages_numbered.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:

#  footer pages: 1-4
#  roman pages: 2-4

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