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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_SigInfo
long li_Validated

li_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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/invoice_signed.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

Write-Debug "Signatures: " + string(loo_Pdf.NumSignatures)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("labels[0].label","Invoice Number:")
loo_Json.UpdateString("labels[0].value","INV-2026-0500")

//  "check" reads only, so it works on a signed PDF.
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.TextReplace("check",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "check: ok = " + string(loo_JsonOut.BoolOf("ok"))

//  "apply" is refused while PreserveSignatures is true (the default).
li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug "apply refused: " + loo_Pdf.LastErrorText
end if

//  Allowing the signature to be broken:
loo_Pdf.PreserveSignatures = 0
li_Success = loo_Pdf.TextReplace("apply",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "applied: " + string(loo_JsonOut.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.
li_Success = loo_Pdf.WriteFile("qa_output/invoice_reissued_broken_signature.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

Write-Debug "Signatures after writing: " + string(loo_Pdf.NumSignatures)
loo_SigInfo = create oleobject
li_rc = loo_SigInfo.ConnectToNewObject("Chilkat.JsonObject")

li_Validated = loo_Pdf.VerifySignature(0,loo_SigInfo)
Write-Debug "Signature 0 verifies: " + string(li_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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_SigInfo