Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
sigInfo: HCkJsonObject;
validated: Boolean;
begin
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 := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/invoice_signed.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('Signatures: ' + IntToStr(CkPdf_getNumSignatures(pdf)));
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'labels[0].label','Invoice Number:');
CkJsonObject_UpdateString(json,'labels[0].value','INV-2026-0500');
// "check" reads only, so it works on a signed PDF.
jsonOut := CkJsonObject_Create();
success := CkPdf_TextReplace(pdf,'check',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('check: ok = ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'ok'))));
// "apply" is refused while PreserveSignatures is true (the default).
success := CkPdf_TextReplace(pdf,'apply',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add('apply refused: ' + CkPdf__lastErrorText(pdf));
end;
// Allowing the signature to be broken:
CkPdf_putPreserveSignatures(pdf,False);
success := CkPdf_TextReplace(pdf,'apply',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('applied: ' + IntToStr(Ord(CkJsonObject_BoolOf(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_WriteFile(pdf,'qa_output/invoice_reissued_broken_signature.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('Signatures after writing: ' + IntToStr(CkPdf_getNumSignatures(pdf)));
sigInfo := CkJsonObject_Create();
validated := CkPdf_VerifySignature(pdf,0,sigInfo);
Memo1.Lines.Add('Signature 0 verifies: ' + IntToStr(Ord(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_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkJsonObject_Dispose(sigInfo);