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

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

  final filter = CkJsonObject();
  final jsonOut = CkJsonObject();

  // What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
  try {
    pdf.watermark('list', filter, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('watermarks: ${jsonOut.intOf('count')}');
  jsonOut.i = 0;
  print('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.
  final json = CkJsonObject();
  json.updateString('type', 'text');
  json.updateString('text', 'SUPERSEDED');
  json.updateString('id', 'superseded');
  try {
    pdf.watermark('add', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Remove only what Acrobat put there, leaving ours in place.
  filter.updateString('source', 'acrobat');
  try {
    pdf.watermark('remove', filter, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('removed (acrobat): ${jsonOut.intOf('removed')}');

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

  print('left in the document: ${jsonOut.intOf('count')}');

  // An empty filter is refused for remove: say all explicitly to strip everything.
  try {
    pdf.watermark('remove', empty, jsonOut);
  } on ChilkatException {
    print('An empty filter was refused, as it should be.');
  }

  empty.updateBool('all', true);
  try {
    pdf.watermark('remove', empty, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('removed (all): ${jsonOut.intOf('removed')}');

  // The changes are in memory until the file is written.
  try {
    pdf.writeFile('qa_output/acrobat_unwatermarked.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // 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
}