Delphi DLL Requires Chilkat v11.7.0+
Delphi DLL
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.Chilkat Delphi DLL Downloads
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
i: Integer;
count_i: Integer;
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_template.pdf
pdf := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/invoice_template.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
// Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'placeholders.customer','Acme Corp');
CkJsonObject_UpdateString(json,'placeholders.date','2026-10-18');
CkJsonObject_UpdateString(json,'placeholders.qty1','2');
CkJsonObject_UpdateString(json,'placeholders.desc1','Cordless drill, 18 V');
CkJsonObject_UpdateString(json,'placeholders.price1','$460.00');
CkJsonObject_UpdateString(json,'placeholders.amount1','$920.00');
CkJsonObject_UpdateString(json,'placeholders.subtotal','$920.00');
CkJsonObject_UpdateString(json,'placeholders.taxRate','7.35%');
CkJsonObject_UpdateString(json,'placeholders.tax','$67.62');
CkJsonObject_UpdateString(json,'placeholders.total','$987.62');
// "check" reports what "apply" would do, without changing the 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('ok: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'ok'))));
Memo1.Lines.Add('would replace: ' + IntToStr(CkJsonObject_SizeOfArray(jsonOut,'replacements')) + ' placeholders');
// Each problem names the placeholder (field) or label, and a reason:
// notFound - the placeholder or label is not on the selected pages
// unusedPlaceholder - a placeholder on the page was given no value
// missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
// placeholderSpansFonts - the text is drawn with more than one font or size
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonOut,'problems');
while i < count_i do
begin
CkJsonObject_putI(jsonOut,i);
Memo1.Lines.Add('problem: ' + CkJsonObject__stringOf(jsonOut,'problems[i].field') + CkJsonObject__stringOf(jsonOut,'problems[i].label') + ' - ' + CkJsonObject__stringOf(jsonOut,'problems[i].reason')
+ ' (page ' + IntToStr(CkJsonObject_IntOf(jsonOut,'problems[i].page')) + ')');
i := i + 1;
end;
// Give the missing placeholders their values and check again.
CkJsonObject_UpdateString(json,'placeholders.po','PO-88213');
CkJsonObject_UpdateString(json,'placeholders.qty2','0');
CkJsonObject_UpdateString(json,'placeholders.desc2','(no second item)');
CkJsonObject_UpdateString(json,'placeholders.price2','');
CkJsonObject_UpdateString(json,'placeholders.amount2','');
success := CkPdf_TextReplace(pdf,'check',json,jsonOut);
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
Memo1.Lines.Add('ok after adding the values: ' + IntToStr(Ord(CkJsonObject_BoolOf(jsonOut,'ok'))));
// Now apply.
if (CkJsonObject_BoolOf(jsonOut,'ok') = True) then
begin
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'))));
success := CkPdf_WriteFile(pdf,'qa_output/invoice_checked.pdf');
if (success = False) then
begin
Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
Exit;
end;
end;
// Sample output:
// ok: false
// would replace: 10 placeholders
// problem: po - unusedPlaceholder (page 1)
// problem: qty2 - unusedPlaceholder (page 1)
// problem: desc2 - unusedPlaceholder (page 1)
// problem: price2 - unusedPlaceholder (page 1)
// problem: amount2 - unusedPlaceholder (page 1)
// ok after adding the values: true
// applied: true
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);