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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vSigInfo
    Handle hoSigInfo
    Boolean iValidated
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/invoice_signed.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumSignatures Of hoPdf To iTemp1
    Showln "Signatures: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "labels[0].label" "Invoice Number:" To iSuccess
    Get ComUpdateString Of hoJson "labels[0].value" "INV-2026-0500" To iSuccess

    //  "check" reads only, so it works on a signed PDF.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "check" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "ok" To bTemp1
    Showln "check: ok = " bTemp1

    //  "apply" is refused while PreserveSignatures is true (the default).
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "apply" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln "apply refused: " sTemp1
    End

    //  Allowing the signature to be broken:
    Set ComPreserveSignatures Of hoPdf To False
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComTextReplace Of hoPdf "apply" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "applied" To bTemp1
    Showln "applied: " bTemp1

    //  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.
    Get ComWriteFile Of hoPdf "qa_output/invoice_reissued_broken_signature.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumSignatures Of hoPdf To iTemp1
    Showln "Signatures after writing: " iTemp1
    Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo
    If (Not(IsComObjectCreated(hoSigInfo))) Begin
        Send CreateComObject of hoSigInfo
    End
    Get pvComObject of hoSigInfo to vSigInfo
    Get ComVerifySignature Of hoPdf 0 vSigInfo To iValidated
    Showln "Signature 0 verifies: " iValidated

    //  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


End_Procedure