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

Remove a Watermark from a PDF

See more PDF Watermark Examples

Demonstrates remove, which deletes the watermarks matching the filter and reports how many went. The filter is the same one list takes, so the usual sequence is to list what is there and then remove by id, by source, or by text. An empty filter is refused unless "all" is true, so that a whole document cannot be stripped by an empty JSON object. Removing finds nothing is not an error: "removed" is simply 0. An annotation is deleted, the marked content of an artifact and any form it alone used are deleted, and the content of a layer goes with the layer once nothing else uses it. A watermark added with the content approach is part of the page and is never removable.

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;
  json: TJsonObject;
  empty: 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_watermark.pdf

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

  filter := TJsonObject.Create;
  jsonOut := TJsonObject.Create;

  //  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
  success := pdf.Watermark('list',filter,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('watermarks: ' + jsonOut.IntOf('count'));
  jsonOut.I := 0;
  WriteLn('first id: ' + jsonOut.StringOf('watermarks[i].id') + ' text: ' + jsonOut.StringOf('watermarks[i].text'));

  //  Add one of our own beside it, so that the filters below have two to choose between.
  json := TJsonObject.Create;
  json.UpdateString('type','text');
  json.UpdateString('text','SUPERSEDED');
  json.UpdateString('id','superseded');
  success := pdf.Watermark('add',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  Remove only what Acrobat put there, leaving ours in place.
  filter.UpdateString('source','acrobat');
  success := pdf.Watermark('remove',filter,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('removed (acrobat): ' + jsonOut.IntOf('removed'));

  empty := TJsonObject.Create;
  success := pdf.Watermark('detect',empty,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('left in the document: ' + jsonOut.IntOf('count'));

  //  An empty filter is refused for remove: say all explicitly to strip everything.
  success := pdf.Watermark('remove',empty,jsonOut);
  if (success = False) then
    begin
      WriteLn('An empty filter was refused, as it should be.');
    end;
  empty.UpdateBool('all',True);
  success := pdf.Watermark('remove',empty,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('removed (all): ' + jsonOut.IntOf('removed'));

  //  The changes are in memory until the file is written.
  success := pdf.WriteFile('qa_output/acrobat_unwatermarked.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  Sample output:

  //  watermarks: 1
  //  first id: ext1 text: Sample
  //  removed (acrobat): 1
  //  left in the document: 1
  //  An empty filter was refused, as it should be.
  //  removed (all): 1


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