Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

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 = CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/invoice_signed.pdf")
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("signatures: " & pdf.NumSignatures)

set json = CreateObject("Chilkat.JsonObject")
set jsonOut = CreateObject("Chilkat.JsonObject")
success = json.UpdateString("type","text")
success = json.UpdateString("text","PAID")
success = json.UpdateString("id","paid")
success = json.UpdateString("color","#2E7D32")
success = json.UpdateString("position.align","topRight")
success = json.UpdateNumber("position.margin","36")

'  An artifact watermark changes the page content, so it is refused while the signatures matter.
success = json.UpdateString("approach","artifact")
success = pdf.Watermark("add",json,jsonOut)
If (success = 0) Then
    outFile.WriteLine("The artifact approach was refused on a signed PDF, as it should be.")
End If

'  An annotation is added without touching the signed bytes.
success = json.UpdateString("approach","annotation")
success = pdf.Watermark("add",json,jsonOut)
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("annotation added, pages: " & jsonOut.StringOf("pages"))

success = pdf.WriteFile("qa_output/invoice_paid.pdf")
If (success = 0) Then
    outFile.WriteLine(pdf.LastErrorText)
    WScript.Quit
End If

'  The signature still verifies: the original bytes are unchanged and the annotation was appended.
set check = CreateObject("Chilkat.Pdf")
success = check.LoadFile("qa_output/invoice_paid.pdf")
If (success = 0) Then
    outFile.WriteLine(check.LastErrorText)
    WScript.Quit
End If

set sigInfo = CreateObject("Chilkat.JsonObject")
verified = check.VerifySignature(0,sigInfo)
outFile.WriteLine("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

outFile.Close