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

Place and Size a PDF Watermark: Alignment, Margins and Fitting

See more PDF Watermark Examples

Demonstrates where the stamp goes and how large it is drawn. position.align picks one of nine spots, position.margin insets it from the page edges, and position.offsetX and offsetY nudge it from there. For the size, scale.factor multiplies the natural size, scale.fit fits the rotated stamp inside the margin box (none, page, width or height) with scale.fitPercent saying how much of that box to fill, and scale.maxWidth and maxHeight cap the result. The units member applies to all of the measurements except font size, which is always in points.

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]

set jsonOut [new_CkJsonObject]

#  A banner across the top, inset three quarters of an inch, filling 60% of the width.
CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "REVIEW COPY"
CkJsonObject_UpdateString $json "id" "banner"
CkJsonObject_UpdateString $json "units" "in"
CkJsonObject_UpdateString $json "position.align" "top"
CkJsonObject_UpdateNumber $json "position.margin" "0.75"
CkJsonObject_UpdateString $json "scale.fit" "width"
CkJsonObject_UpdateNumber $json "scale.fitPercent" "60"
CkJsonObject_UpdateString $json "color" "#1F4E79"
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 "banner pages: [CkJsonObject_stringOf $jsonOut pages]"

#  A second stamp in the bottom-left corner, nudged a quarter inch to the right and up, with its
#  size capped rather than fitted.
set json2 [new_CkJsonObject]

CkJsonObject_UpdateString $json2 "type" "text"
CkJsonObject_UpdateString $json2 "text" "Not for distribution"
CkJsonObject_UpdateString $json2 "id" "footnote"
CkJsonObject_UpdateString $json2 "units" "in"
CkJsonObject_UpdateString $json2 "position.align" "bottomLeft"
CkJsonObject_UpdateNumber $json2 "position.margin" "0.5"
CkJsonObject_UpdateNumber $json2 "position.offsetX" "0.25"
CkJsonObject_UpdateNumber $json2 "position.offsetY" "-0.25"
CkJsonObject_UpdateNumber $json2 "font.size" "9"
CkJsonObject_UpdateNumber $json2 "scale.maxWidth" "3.0"
set success [CkPdf_Watermark $pdf "add" $json2 $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json2
    exit
}

puts "footnote pages: [CkJsonObject_stringOf $jsonOut pages]"

#  Each call adds one watermark, so a banner plus a note is two calls.
set success [CkPdf_WriteFile $pdf "qa_output/four_pages_placed.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json2
    exit
}

#  Sample output:

#  banner pages: 1-4
#  footnote pages: 1-4

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