Unicode C Requires Chilkat v11.7.0+
Unicode C
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 Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
HCkJsonObjectW sigInfo;
BOOL validated;
success = FALSE;
// 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 = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/invoice_signed.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
wprintf(L"Signatures: %d\n",CkPdfW_getNumSignatures(pdf));
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"labels[0].label",L"Invoice Number:");
CkJsonObjectW_UpdateString(json,L"labels[0].value",L"INV-2026-0500");
// "check" reads only, so it works on a signed PDF.
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_TextReplace(pdf,L"check",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"check: ok = %d\n",CkJsonObjectW_BoolOf(jsonOut,L"ok"));
// "apply" is refused while PreserveSignatures is true (the default).
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
if (success == FALSE) {
wprintf(L"apply refused: %s\n",CkPdfW_lastErrorText(pdf));
}
// Allowing the signature to be broken:
CkPdfW_putPreserveSignatures(pdf,FALSE);
success = CkPdfW_TextReplace(pdf,L"apply",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"applied: %d\n",CkJsonObjectW_BoolOf(jsonOut,L"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 = CkPdfW_WriteFile(pdf,L"qa_output/invoice_reissued_broken_signature.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Signatures after writing: %d\n",CkPdfW_getNumSignatures(pdf));
sigInfo = CkJsonObjectW_Create();
validated = CkPdfW_VerifySignature(pdf,0,sigInfo);
wprintf(L"Signature 0 verifies: %d\n",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
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
CkJsonObjectW_Dispose(sigInfo);
}