Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oCheck
LOCAL oSigInfo
LOCAL nVerified
nSuccess := 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
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/invoice_signed.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
ENDIF
? "signatures: " + Str(oPdf:NumSignatures)
oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "PAID")
oJson:UpdateString("id", "paid")
oJson:UpdateString("color", "#2E7D32")
oJson:UpdateString("position.align", "topRight")
oJson:UpdateNumber("position.margin", "36")
// An artifact watermark changes the page content, so it is refused while the signatures matter.
oJson:UpdateString("approach", "artifact")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
? "The artifact approach was refused on a signed PDF, as it should be."
ENDIF
// An annotation is added without touching the signed bytes.
oJson:UpdateString("approach", "annotation")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
? "annotation added, pages: " + oJsonOut:StringOf("pages")
nSuccess := oPdf:WriteFile("qa_output/invoice_paid.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// The signature still verifies: the original bytes are unchanged and the annotation was appended.
oCheck := CreateObject("Chilkat.Pdf")
nSuccess := oCheck:LoadFile("qa_output/invoice_paid.pdf")
IF (nSuccess == 0)
? oCheck:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oCheck:destroy()
RETURN
ENDIF
oSigInfo := CreateObject("Chilkat.JsonObject")
nVerified := oCheck:VerifySignature(0, oSigInfo)
? "signature still verifies: " + Str(nVerified)
// 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
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oCheck:destroy()
oSigInfo:destroy()