Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Replace the Second (or Every) Occurrence of a Label in a PDF
See more PDF Text Replacement Examples
Demonstrates the "occurrence" member of a label entry. By default the first occurrence of a label, in reading order across the selected pages, is replaced; "occurrence" can be a number for the nth, or "all" for every occurrence. The invoice template has "Ref:" twice.Chilkat Lazarus Pascal Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Pdf,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pdf: TPdf;
json: TJsonObject;
jsonOut: TJsonObject;
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 := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/invoice_template.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
// This template's {{placeholders}} are not being filled here: "keep" leaves them as they are
// (the default, "error", would refuse to apply while a placeholder on the page has no value).
json.UpdateString('unusedPlaceholders','keep');
// "Ref:" appears twice on the page (A-1 near the top, A-2 below the totals).
// The default replaces the first occurrence only.
json.UpdateString('labels[0].label','Ref:');
json.UpdateString('labels[0].value','B-7');
success := pdf.TextReplace('check',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('default: ' + jsonOut.IntOf('replacements[0].count') + ' occurrence replaced');
// The second occurrence only.
json.UpdateString('labels[0].occurrence','2');
success := pdf.TextReplace('check',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('occurrence 2: ' + jsonOut.IntOf('replacements[0].count') + ' occurrence replaced');
// Every occurrence.
json.UpdateString('labels[0].occurrence','all');
success := pdf.TextReplace('apply',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('occurrence all: ' + jsonOut.IntOf('replacements[0].count') + ' occurrences replaced');
success := pdf.WriteFile('qa_output/invoice_refs.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// Sample output:
// default: 1 occurrence replaced
// occurrence 2: 1 occurrence replaced
// occurrence all: 2 occurrences replaced
pdf.Free;
json.Free;
jsonOut.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.