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

Replace Text in a Signed PDF (PreserveSignatures)

See more PDF Text Replacement Examples

Applying a text replacement rewrites the page content, which invalidates any digital signature on the PDF. While the PreserveSignatures property is true (the default), "apply" is refused on a signed PDF; "check" only reads and is always allowed. Setting PreserveSignatures to false allows the replacement; the signature field stays in the written PDF but no longer verifies, so the document should be signed again afterwards if a signature is wanted.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loSigInfo
LOCAL lnValidated

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')
loJson.UpdateString("labels[0].label","Invoice Number:")
loJson.UpdateString("labels[0].value","INV-2026-0500")

*  "check" reads only, so it works on a signed PDF.
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.TextReplace("check",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "check: ok = " + STR(loJsonOut.BoolOf("ok"))

*  "apply" is refused while PreserveSignatures is true (the default).
lnSuccess = loPdf.TextReplace("apply",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? "apply refused: " + loPdf.LastErrorText
ENDIF

*  Allowing the signature to be broken:
loPdf.PreserveSignatures = 0
lnSuccess = loPdf.TextReplace("apply",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "applied: " + STR(loJsonOut.BoolOf("applied"))

*  The written PDF is a full rewrite with the replacement.  The signature field is still in the document,
*  but the bytes it signed are gone, so it no longer verifies.
lnSuccess = loPdf.WriteFile("qa_output/invoice_reissued_broken_signature.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "Signatures after writing: " + STR(loPdf.NumSignatures)
loSigInfo = CreateObject('Chilkat.JsonObject')
lnValidated = loPdf.VerifySignature(0,loSigInfo)
? "Signature 0 verifies: " + STR(lnValidated)

*  Sample output:

*  Signatures: 1
*  check: ok = true
*  apply refused: ChilkatLog:
*    TextReplace:
*      DllDate: Nov  7 2026
*      ChilkatVersion: 11.7.0
*      UnlockPrefix: CHLKAT
*      UnlockStatus: 2
*      Architecture: Little Endian; 64-bit
*      Language: Visual C++ 11.0 / x64
*      VerboseLogging: 0
*      replaceTextOp:
*        op: apply
*        The PDF is signed: replacing text rewrites the content and invalidates the signatures (set PreserveSignatures = false to allow it).
*      --replaceTextOp
*      Failed.
*    --TextReplace
*  --ChilkatLog
*  
*  applied: true
*  Signatures after writing: 1
*  Signature 0 verifies: false

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loSigInfo