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

Find Out Whether a PDF Has a Watermark

See more PDF Watermark Examples

Demonstrates detect, which answers yes or no, and list, which enumerates what is there. Both recognize the watermarks Chilkat writes and the standard forms other tools write: Watermark annotations from any producer, marked content tagged as a Watermark or Background artifact, Acrobat watermarks and backgrounds, and optional-content layers whose name says watermark. Acrobat headers and footers are not watermarks and are ignored. A watermark from another producer has no id of its own, so Chilkat assigns one (ext1, ext2, ...) that stays valid until the PDF is changed, which is what makes a list followed by a remove by id work. Properties that cannot be read back are left out rather than guessed.

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;
  filter: TJsonObject;
  jsonOut: TJsonObject;
  i: Integer;
  count_i: Integer;
  bySource: 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 acrobat_two_watermarks.pdf

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

  //  An empty filter means every watermark in the document.
  filter := TJsonObject.Create;
  jsonOut := TJsonObject.Create;
  success := pdf.Watermark('detect',filter,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('found: ' + jsonOut.BoolOf('found') + ' count: ' + jsonOut.IntOf('count'));

  success := pdf.Watermark('list',filter,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  i := 0;
  count_i := jsonOut.SizeOfArray('watermarks');
  while i < count_i do
    begin
      jsonOut.I := i;
      WriteLn('id: ' + jsonOut.StringOf('watermarks[i].id'));
      WriteLn('   source:    ' + jsonOut.StringOf('watermarks[i].source'));
      WriteLn('   approach:  ' + jsonOut.StringOf('watermarks[i].approach'));
      WriteLn('   type:      ' + jsonOut.StringOf('watermarks[i].type'));
      WriteLn('   text:      ' + jsonOut.StringOf('watermarks[i].text'));
      WriteLn('   pages:     ' + jsonOut.StringOf('watermarks[i].pages'));
      WriteLn('   zOrder:    ' + jsonOut.StringOf('watermarks[i].zOrder'));
      WriteLn('   removable: ' + jsonOut.BoolOf('watermarks[i].removable'));
      i := i + 1;
    end;

  //  The filter narrows what is reported: by producer (chilkat, acrobat or other), by approach, by
  //  pages, by an exact id or label, or by text with textContains.
  bySource := TJsonObject.Create;
  bySource.UpdateString('source','acrobat');
  success := pdf.Watermark('detect',bySource,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('made by Acrobat: ' + jsonOut.IntOf('count'));

  //  Sample output:

  //  found: True count: 2
  //  id: ext1
  //     source:    acrobat
  //     approach:  artifact
  //     type:      image
  //     text:
  //     pages:     1
  //     zOrder:    front
  //     removable: True
  //  id: ext2
  //     source:    acrobat
  //     approach:  artifact
  //     type:      text
  //     text:      sample
  //     pages:     1
  //     zOrder:    front
  //     removable: True
  //  made by Acrobat: 2


  pdf.Free;
  filter.Free;
  jsonOut.Free;
  bySource.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.