Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Replace the Value After a Label in a PDF (Re-issue an Invoice)
See more PDF Text Replacement Examples
Demonstrates replacing the values that follow labels such as "Invoice Number:" on a PDF that has no placeholders -- an ordinary invoice. Each entry of the "labels" array names a label as it appears on the page and the new value; the value replaces the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon. The replaced text is read back with ExtractText to confirm it.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;
i: Integer;
count_i: Integer;
jsonRead: TJsonObject;
jsonText: 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_labels.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/invoice_labels.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// The invoice has lines such as "Invoice Number: INV-2026-0412" and "Total Due: $1,234.53".
// Each label's new value replaces whatever follows the label on its line.
json := TJsonObject.Create;
json.UpdateString('labels[0].label','Invoice Number:');
json.UpdateString('labels[0].value','INV-2026-0500');
json.UpdateString('labels[1].label','Invoice Date:');
json.UpdateString('labels[1].value','April 2, 2026');
json.UpdateString('labels[2].label','customer');
json.UpdateString('labels[2].value','Contoso Ltd');
json.UpdateString('labels[3].label','Total Due:');
json.UpdateString('labels[3].value','$1,999.99');
jsonOut := TJsonObject.Create;
success := pdf.TextReplace('apply',json,jsonOut);
if (success = False) then
begin
WriteLn('Not applied: ' + jsonOut.Emit());
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
end;
i := 0;
count_i := jsonOut.SizeOfArray('replacements');
while i < count_i do
begin
jsonOut.I := i;
WriteLn('Replaced the value of "' + jsonOut.StringOf('replacements[i].label') + '" on page ' + jsonOut.StringOf('replacements[i].pages'));
i := i + 1;
end;
success := pdf.WriteFile('qa_output/invoice_reissued.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
// Read the new values back with ExtractText's label option.
jsonRead := TJsonObject.Create;
jsonText := TJsonObject.Create;
jsonRead.UpdateString('label','Total Due');
jsonRead.UpdateString('labelValue','right');
success := pdf.ExtractText('text',jsonRead,jsonText);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('Total Due is now: ' + jsonText.StringOf('text'));
// Sample output:
// Replaced the value of "Invoice Number:" on page 1
// Replaced the value of "Invoice Date:" on page 1
// Replaced the value of "customer" on page 1
// Replaced the value of "Total Due:" on page 1
// Total Due is now: $1,999.99
pdf.Free;
json.Free;
jsonOut.Free;
jsonRead.Free;
jsonText.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.