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

A PDF Watermark That Only Appears When Printed

See more PDF Watermark Examples

Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.

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

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

  json := TJsonObject.Create;
  json.UpdateString('type','text');
  json.UpdateString('text','UNCONTROLLED WHEN PRINTED');
  json.UpdateString('id','printonly');

  //  As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
  json.UpdateString('approach','layer');
  json.UpdateString('layer.name','Print marking');
  json.UpdateString('layer.view','off');
  json.UpdateString('layer.print','on');
  json.UpdateBool('layer.locked',True);

  json.UpdateString('rotation','diagonal');
  json.UpdateString('scale.fit','page');
  json.UpdateNumber('scale.fitPercent','70');
  json.UpdateNumber('opacity','0.3');

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

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

  //  The same thing as an annotation instead.
  pdf2 := TPdf.Create;
  success := pdf2.LoadFile('qa_data/pdf/two_page_memo.pdf');
  if (success = False) then
    begin
      WriteLn(pdf2.LastErrorText);
      Exit;
    end;
  json2 := TJsonObject.Create;
  json2.UpdateString('type','text');
  json2.UpdateString('text','UNCONTROLLED WHEN PRINTED');
  json2.UpdateString('id','printonly2');
  json2.UpdateString('approach','annotation');
  json2.UpdateBool('annotation.view',False);
  json2.UpdateBool('annotation.print',True);
  json2.UpdateBool('annotation.fixedPrint',True);
  json2.UpdateString('rotation','diagonal');
  success := pdf2.Watermark('add',json2,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf2.LastErrorText);
      Exit;
    end;
  WriteLn('annotation id: ' + jsonOut.StringOf('id'));

  //  Sample output:

  //  layer id: printonly pages: 1-2
  //  annotation id: printonly2


  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.