Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

PDF Placeholders Without a Value: Error, Blank or Keep

See more PDF Text Replacement Examples

Demonstrates the "unusedPlaceholders" option, which decides what happens to a placeholder found on the page that has no value in "placeholders": "error" (the default) makes it a problem with the reason unusedPlaceholder, so nothing is applied; "blank" replaces it with nothing; "keep" leaves it on the page as it is.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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;

  //  Values for every placeholder except {{po}} (the purchase order number, which this customer has none of).
  json := TJsonObject.Create;
  json.UpdateString('placeholders.customer','Acme Corp');
  json.UpdateString('placeholders.date','2026-10-18');
  json.UpdateString('placeholders.qty1','2');
  json.UpdateString('placeholders.desc1','Cordless drill, 18 V');
  json.UpdateString('placeholders.price1','$460.00');
  json.UpdateString('placeholders.amount1','$920.00');
  json.UpdateString('placeholders.qty2','6');
  json.UpdateString('placeholders.desc2','Galvanized hinge, 4 inch');
  json.UpdateString('placeholders.price2','$12.50');
  json.UpdateString('placeholders.amount2','$75.00');
  json.UpdateString('placeholders.subtotal','$995.00');
  json.UpdateString('placeholders.taxRate','7.35%');
  json.UpdateString('placeholders.tax','$73.13');
  json.UpdateString('placeholders.total','$1,068.13');

  jsonOut := TJsonObject.Create;

  //  The default, "error": the unfilled {{po}} is a problem and nothing would be applied.
  success := pdf.TextReplace('check',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('error (default): ok = ' + jsonOut.BoolOf('ok') + ', problem: ' + jsonOut.StringOf('problems[0].field') + ' ' + jsonOut.StringOf('problems[0].reason'));

  //  "keep": {{po}} stays on the page, literally.
  json.UpdateString('unusedPlaceholders','keep');
  success := pdf.TextReplace('check',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('keep: ok = ' + jsonOut.BoolOf('ok'));

  //  "blank": {{po}} is replaced by nothing, leaving "PO Number:" with an empty value.
  json.UpdateString('unusedPlaceholders','blank');
  success := pdf.TextReplace('apply',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('blank: applied = ' + jsonOut.BoolOf('applied'));

  success := pdf.WriteFile('qa_output/invoice_no_po.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  Sample output:

  //  error (default): ok = false, problem: po unusedPlaceholder
  //  keep: ok = true
  //  blank: applied = true


  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.