PureBasic Requires Chilkat v11.7.0+
PureBasic
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 PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 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
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/invoice_signed.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
Debug "Signatures: " + Str(CkPdf::ckNumSignatures(pdf))
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckUpdateString(json,"labels[0].label","Invoice Number:")
CkJsonObject::ckUpdateString(json,"labels[0].value","INV-2026-0500")
; "check" reads only, so it works on a signed PDF.
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckTextReplace(pdf,"check",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "check: ok = " + Str(CkJsonObject::ckBoolOf(jsonOut,"ok"))
; "apply" is refused while PreserveSignatures is true (the default).
success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
If success = 0
Debug "apply refused: " + CkPdf::ckLastErrorText(pdf)
EndIf
; Allowing the signature to be broken:
CkPdf::setCkPreserveSignatures(pdf, 0)
success = CkPdf::ckTextReplace(pdf,"apply",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "applied: " + Str(CkJsonObject::ckBoolOf(jsonOut,"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.
success = CkPdf::ckWriteFile(pdf,"qa_output/invoice_reissued_broken_signature.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "Signatures after writing: " + Str(CkPdf::ckNumSignatures(pdf))
sigInfo.i = CkJsonObject::ckCreate()
If sigInfo.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
validated.i = CkPdf::ckVerifySignature(pdf,0,sigInfo)
Debug "Signature 0 verifies: " + Str(validated)
; 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
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkJsonObject::ckDispose(sigInfo)
ProcedureReturn
EndProcedure