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

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

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

  final pdf = CkPdf();

  // Changing the permissions needs owner rights.  Both passwords are given here, which is what
  // an AES-256 PDF requires for this operation.
  pdf.userPassword = 'secret';
  pdf.ownerPassword = 'owner-secret';
  try {
    pdf.loadFile('qa_data/pdf/encrypted_user_password.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // No printing and no copying.  Everything not mentioned is granted.
  final json = CkJsonObject();
  json.updateString('permissions.print', 'none');
  json.updateBool('permissions.copy', false);

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

  print('print: ${jsonOut.stringOf('pending.permissions.print')}');
  print('copy: ${jsonOut.boolOf('pending.permissions.copy')}');
  print('modify: ${jsonOut.boolOf('pending.permissions.modify')}');
  print('password still needed to open: ${jsonOut.boolOf('pending.userPasswordRequired')}');

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

  // The same user password opens the result, now with the new permissions.
  final reader = CkPdf();
  reader.userPassword = 'secret';
  try {
    reader.loadFile('qa_output/minutes_no_print.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('saved file, print: ${info.stringOf('loaded.permissions.print')}');
  print('saved file, copy: ${info.boolOf('loaded.permissions.copy')}');

  // Sample output:

  // print: none
  // copy: False
  // modify: True
  // password still needed to open: True
  // saved file, print: none
  // saved file, copy: False
}