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

Restrict Printing, Copying and Editing of a PDF

See more PDF Protection Examples

Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.

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

  // No user password: the PDF opens without a prompt.  The owner password is what protects the
  // restrictions, so it must not be empty when anything is restricted.
  pdf.userPassword = '';
  pdf.ownerPassword = 'owner-secret';

  final json = CkJsonObject();
  json.updateString('permissions.print', 'lowQuality');
  json.updateBool('permissions.copy', false);
  json.updateBool('permissions.modify', false);
  json.updateBool('permissions.annotate', false);
  // The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

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

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

  // Anyone can open the result, with the rights of a user.
  final reader = CkPdf();
  try {
    reader.loadFile('qa_output/memo_restricted.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final empty = CkJsonObject();
  final info = CkJsonObject();
  try {
    reader.protection('getInfo', empty, info);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('opened with: ${info.stringOf('loaded.openedWith')}');
  print('print: ${info.stringOf('loaded.permissions.print')}');
  print('copy: ${info.boolOf('loaded.permissions.copy')}');
  print('modify: ${info.boolOf('loaded.permissions.modify')}');

  // Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
  final opts = CkJsonObject();
  final extractOut = CkJsonObject();
  final sb = CkStringBuilder();
  try {
    reader.extractTextSb('text', opts, sb, extractOut);
  } on ChilkatException {
    print('Text extraction was refused: the PDF does not permit copying.');
  }

  // The owner password lifts every restriction.
  final owner = CkPdf();
  owner.ownerPassword = 'owner-secret';
  try {
    owner.loadFile('qa_output/memo_restricted.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final ownerInfo = CkJsonObject();
  try {
    owner.protection('getInfo', empty, ownerInfo);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('opened with: ${ownerInfo.stringOf('loaded.openedWith')}');
  try {
    owner.extractTextSb('text', opts, sb, extractOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('characters extracted with the owner password: ${sb.length}');

  // Sample output:

  // opened with: emptyPassword
  // print: lowQuality
  // copy: False
  // modify: False
  // Text extraction was refused: the PDF does not permit copying.
  // opened with: owner
  // characters extracted with the owner password: 1048
}