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

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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

  final pdf = CkPdf();
  try {
    pdf.loadFile('qa_data/pdf/two_page_memo.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final json = CkJsonObject();
  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');

  final jsonOut = CkJsonObject();
  try {
    pdf.watermark('add', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('layer id: ${jsonOut.stringOf('id')} pages: ${jsonOut.stringOf('pages')}');

  try {
    pdf.writeFile('qa_output/memo_print_only.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The same thing as an annotation instead.
  final pdf2 = CkPdf();
  try {
    pdf2.loadFile('qa_data/pdf/two_page_memo.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final json2 = CkJsonObject();
  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');
  try {
    pdf2.watermark('add', json2, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('annotation id: ${jsonOut.stringOf('id')}');

  // Sample output:

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