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

Add a CONFIDENTIAL Watermark to Every Page of a PDF

See more PDF Watermark Examples

Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.

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 long_report.pdf

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

  json := TJsonObject.Create;
  json.UpdateString('type','text');
  json.UpdateString('text','CONFIDENTIAL');

  //  An id of your own choosing, so that the watermark can be found and removed later.  One is
  //  generated when you do not give one.
  json.UpdateString('id','confidential');

  json.UpdateString('color','#C00000');
  json.UpdateNumber('opacity','0.25');
  json.UpdateString('rotation','diagonal');
  json.UpdateString('scale.fit','page');
  json.UpdateNumber('scale.fitPercent','80');

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

  //  The id, and the pages it was stamped on.  "pages" defaults to all.
  WriteLn('id: ' + jsonOut.StringOf('id'));
  WriteLn('pages: ' + jsonOut.StringOf('pages'));

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

  //  Sample output:

  //  id: confidential
  //  pages: 1-3


  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.