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

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

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;
  pdf2: TPdf;
  json2: 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 four_pages.pdf

  pdf := TPdf.Create;
  success := pdf.LoadFile('qa_data/pdf/four_pages.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  json.UpdateString('type','text');
  json.UpdateString('text','Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}');
  json.UpdateString('id','pagefoot');
  json.UpdateString('position.align','bottom');
  json.UpdateNumber('position.margin','24');
  json.UpdateNumber('font.size','9');
  json.UpdateString('color','#505050');

  jsonOut := TJsonObject.Create;
  success := pdf.Watermark('add',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('footer pages: ' + jsonOut.StringOf('pages'));

  //  Roman numerals that start at i on page 2, so that a cover page is not numbered.
  pdf2 := TPdf.Create;
  success := pdf2.LoadFile('qa_data/pdf/four_pages.pdf');
  if (success = False) then
    begin
      WriteLn(pdf2.LastErrorText);
      Exit;
    end;
  json2 := TJsonObject.Create;
  json2.UpdateString('type','text');
  json2.UpdateString('text','- {page} -');
  json2.UpdateString('id','roman');
  json2.UpdateString('pages','2-');
  json2.UpdateInt('pageNumber.start',1);
  json2.UpdateString('pageNumber.style','romanLower');
  json2.UpdateString('position.align','bottom');
  json2.UpdateNumber('font.size','10');
  success := pdf2.Watermark('add',json2,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf2.LastErrorText);
      Exit;
    end;
  WriteLn('roman pages: ' + jsonOut.StringOf('pages'));

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

  //  Sample output:

  //  footer pages: 1-4
  //  roman pages: 2-4


  pdf.Free;
  json.Free;
  jsonOut.Free;
  pdf2.Free;
  json2.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.