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

Remove a Watermark from a PDF

See more PDF Watermark Examples

Demonstrates remove, which deletes the watermarks matching the filter and reports how many went. The filter is the same one list takes, so the usual sequence is to list what is there and then remove by id, by source, or by text. An empty filter is refused unless "all" is true, so that a whole document cannot be stripped by an empty JSON object. Removing finds nothing is not an error: "removed" is simply 0. An annotation is deleted, the marked content of an artifact and any form it alone used are deleted, and the content of a layer goes with the layer once nothing else uses it. A watermark added with the content approach is part of the page and is never removable.

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

set pdf [new_CkPdf]

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

set filter [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

#  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
set success [CkPdf_Watermark $pdf "list" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $filter
    delete_CkJsonObject $jsonOut
    exit
}

puts "watermarks: [CkJsonObject_IntOf $jsonOut count]"
CkJsonObject_put_I $jsonOut 0
puts "first id: [CkJsonObject_stringOf $jsonOut {watermarks[i].id}] text: [CkJsonObject_stringOf $jsonOut {watermarks[i].text}]"

#  Add one of our own beside it, so that the filters below have two to choose between.
set json [new_CkJsonObject]

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

#  Remove only what Acrobat put there, leaving ours in place.
CkJsonObject_UpdateString $filter "source" "acrobat"
set success [CkPdf_Watermark $pdf "remove" $filter $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $filter
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json
    exit
}

puts "removed (acrobat): [CkJsonObject_IntOf $jsonOut removed]"

set empty [new_CkJsonObject]

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

puts "left in the document: [CkJsonObject_IntOf $jsonOut count]"

#  An empty filter is refused for remove: say all explicitly to strip everything.
set success [CkPdf_Watermark $pdf "remove" $empty $jsonOut]
if {$success == 0} then {
    puts "An empty filter was refused, as it should be."
}

CkJsonObject_UpdateBool $empty "all" 1
set success [CkPdf_Watermark $pdf "remove" $empty $jsonOut]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $filter
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json
    delete_CkJsonObject $empty
    exit
}

puts "removed (all): [CkJsonObject_IntOf $jsonOut removed]"

#  The changes are in memory until the file is written.
set success [CkPdf_WriteFile $pdf "qa_output/acrobat_unwatermarked.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $pdf]
    delete_CkPdf $pdf
    delete_CkJsonObject $filter
    delete_CkJsonObject $jsonOut
    delete_CkJsonObject $json
    delete_CkJsonObject $empty
    exit
}

#  Sample output:

#  watermarks: 1
#  first id: ext1 text: Sample
#  removed (acrobat): 1
#  left in the document: 1
#  An empty filter was refused, as it should be.
#  removed (all): 1

delete_CkPdf $pdf
delete_CkJsonObject $filter
delete_CkJsonObject $jsonOut
delete_CkJsonObject $json
delete_CkJsonObject $empty