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

Watermark a Signed PDF Without Breaking the Signature

See more PDF Watermark Examples

Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.

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

set pdf [new_CkPdf]

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

puts "signatures: [CkPdf_get_NumSignatures $pdf]"

set json [new_CkJsonObject]

set jsonOut [new_CkJsonObject]

CkJsonObject_UpdateString $json "type" "text"
CkJsonObject_UpdateString $json "text" "PAID"
CkJsonObject_UpdateString $json "id" "paid"
CkJsonObject_UpdateString $json "color" "#2E7D32"
CkJsonObject_UpdateString $json "position.align" "topRight"
CkJsonObject_UpdateNumber $json "position.margin" "36"

#  An artifact watermark changes the page content, so it is refused while the signatures matter.
CkJsonObject_UpdateString $json "approach" "artifact"
set success [CkPdf_Watermark $pdf "add" $json $jsonOut]
if {$success == 0} then {
    puts "The artifact approach was refused on a signed PDF, as it should be."
}

#  An annotation is added without touching the signed bytes.
CkJsonObject_UpdateString $json "approach" "annotation"
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 "annotation added, pages: [CkJsonObject_stringOf $jsonOut pages]"

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

#  The signature still verifies: the original bytes are unchanged and the annotation was appended.
set check [new_CkPdf]

set success [CkPdf_LoadFile $check "qa_output/invoice_paid.pdf"]
if {$success == 0} then {
    puts [CkPdf_lastErrorText $check]
    delete_CkPdf $pdf
    delete_CkJsonObject $json
    delete_CkJsonObject $jsonOut
    delete_CkPdf $check
    exit
}

set sigInfo [new_CkJsonObject]

set verified [CkPdf_VerifySignature $check 0 $sigInfo]
puts "signature still verifies: $verified"

#  Sample output:

#  signatures: 1
#  The artifact approach was refused on a signed PDF, as it should be.
#  annotation added, pages: 1
#  signature still verifies: True

delete_CkPdf $pdf
delete_CkJsonObject $json
delete_CkJsonObject $jsonOut
delete_CkPdf $check
delete_CkJsonObject $sigInfo