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

How a PDF Watermark Is Stored: artifact, annotation, layer or content

See more PDF Watermark Examples

Demonstrates the approach member, which decides how the watermark is stored and therefore what can be done with it afterwards. artifact (the default) is marked content that viewers draw, text extraction leaves out and Chilkat can remove later. annotation is a Watermark annotation, always on top, and the only approach allowed on a signed PDF while PreserveSignatures is true. layer is an optional-content layer the viewer can hide and that can be made print-only. content draws straight into the page with no marker: visible everywhere and the tamper-resistant choice, but by design it can never be listed or removed again.

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

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

set filter [new_CkJsonObject]

#  The same watermark stored four different ways, each in its own copy of the document.
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_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    exit
}

CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "DRAFT"
CkJsonObject_UpdateString $json "id" "draft"
CkJsonObject_UpdateString $json "approach" "artifact"
set success [CkPdf_Watermark $pdf "add" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    exit
}

#  detect answers whether anything matching the filter is in the document.  Finding nothing is
#  still success: "found" is simply false.
CkJsonObject_UpdateString $filter "id" "draft"
set success [CkPdf_Watermark $pdf "detect" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    exit
}

puts "artifact: found = [CkJsonObject_BoolOf $jsonOut found]"

#  An annotation: always drawn on top, and the one approach a signed PDF accepts.
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_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    exit
}

CkJsonObject_UpdateString $json "approach" "annotation"
set success [CkPdf_Watermark $pdf2 "add" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf2]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    exit
}

set success [CkPdf_Watermark $pdf2 "detect" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf2]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    exit
}

puts "annotation: found = [CkJsonObject_BoolOf $jsonOut found]"

#  A layer, which the viewer's layers panel can switch off.
set pdf3 [new_CkPdf]

set success [CkPdf_LoadFile $pdf3 "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf3]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    exit
}

CkJsonObject_UpdateString $json "approach" "layer"
CkJsonObject_UpdateString $json "layer.name" "Draft marking"
set success [CkPdf_Watermark $pdf3 "add" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf3]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    exit
}

set success [CkPdf_Watermark $pdf3 "detect" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf3]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    exit
}

puts "layer: found = [CkJsonObject_BoolOf $jsonOut found]"

#  Drawn into the page content with no marker.  There is nothing to find afterwards, which is the
#  point of it: it cannot be stripped out by a tool that removes watermarks.
set pdf4 [new_CkPdf]

set success [CkPdf_LoadFile $pdf4 "qa_data/pdf/two_page_memo.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf4]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    delete_CkPdf $pdf4
    exit
}

CkJsonObject_UpdateString $json "approach" "content"
set success [CkPdf_Watermark $pdf4 "add" $json $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf4]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    delete_CkPdf $pdf4
    exit
}

set success [CkPdf_Watermark $pdf4 "detect" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf4]
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $filter
    delete_CkPdf $pdf
    delete_CkPdf $pdf2
    delete_CkPdf $pdf3
    delete_CkPdf $pdf4
    exit
}

puts "content: found = [CkJsonObject_BoolOf $jsonOut found]"

#  Sample output:

#  artifact: found = True
#  annotation: found = True
#  layer: found = True
#  content: found = False

delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkJsonObject $filter
delete_CkPdf $pdf
delete_CkPdf $pdf2
delete_CkPdf $pdf3
delete_CkPdf $pdf4