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

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loCheck
LOCAL loSigInfo
LOCAL lnVerified

lnSuccess = 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

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/invoice_signed.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

? "signatures: " + STR(loPdf.NumSignatures)

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("type","text")
loJson.UpdateString("text","PAID")
loJson.UpdateString("id","paid")
loJson.UpdateString("color","#2E7D32")
loJson.UpdateString("position.align","topRight")
loJson.UpdateNumber("position.margin","36")

*  An artifact watermark changes the page content, so it is refused while the signatures matter.
loJson.UpdateString("approach","artifact")
lnSuccess = loPdf.Watermark("add",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? "The artifact approach was refused on a signed PDF, as it should be."
ENDIF

*  An annotation is added without touching the signed bytes.
loJson.UpdateString("approach","annotation")
lnSuccess = loPdf.Watermark("add",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "annotation added, pages: " + loJsonOut.StringOf("pages")

lnSuccess = loPdf.WriteFile("qa_output/invoice_paid.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  The signature still verifies: the original bytes are unchanged and the annotation was appended.
loCheck = CreateObject('Chilkat.Pdf')
lnSuccess = loCheck.LoadFile("qa_output/invoice_paid.pdf")
IF (lnSuccess = 0) THEN
    ? loCheck.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loCheck
    CANCEL
ENDIF

loSigInfo = CreateObject('Chilkat.JsonObject')
lnVerified = loCheck.VerifySignature(0,loSigInfo)
? "signature still verifies: " + STR(lnVerified)

*  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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loCheck
RELEASE loSigInfo