Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
pdf2: HCkPdf;
json2: HCkJsonObject;

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 := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/two_page_memo.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'type','text');
CkJsonObject_UpdateString(json,'text','UNCONTROLLED WHEN PRINTED');
CkJsonObject_UpdateString(json,'id','printonly');

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

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

jsonOut := CkJsonObject_Create();
success := CkPdf_Watermark(pdf,'add',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('layer id: ' + CkJsonObject__stringOf(jsonOut,'id') + ' pages: ' + CkJsonObject__stringOf(jsonOut,'pages'));

success := CkPdf_WriteFile(pdf,'qa_output/memo_print_only.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

//  The same thing as an annotation instead.
pdf2 := CkPdf_Create();
success := CkPdf_LoadFile(pdf2,'qa_data/pdf/two_page_memo.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
    Exit;
  end;
json2 := CkJsonObject_Create();
CkJsonObject_UpdateString(json2,'type','text');
CkJsonObject_UpdateString(json2,'text','UNCONTROLLED WHEN PRINTED');
CkJsonObject_UpdateString(json2,'id','printonly2');
CkJsonObject_UpdateString(json2,'approach','annotation');
CkJsonObject_UpdateBool(json2,'annotation.view',False);
CkJsonObject_UpdateBool(json2,'annotation.print',True);
CkJsonObject_UpdateBool(json2,'annotation.fixedPrint',True);
CkJsonObject_UpdateString(json2,'rotation','diagonal');
success := CkPdf_Watermark(pdf2,'add',json2,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf2));
    Exit;
  end;
Memo1.Lines.Add('annotation id: ' + CkJsonObject__stringOf(jsonOut,'id'));

//  Sample output:

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

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
CkPdf_Dispose(pdf2);
CkJsonObject_Dispose(json2);