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

Encrypt and Sign a PDF, and Sign an Encrypted PDF

See more PDF Protection Examples

Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.

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

  // The signing certificate, with its private key.
  final cert = CkCert();
  try {
    cert.loadPfxFile('qa_data/pfx/myPdfSigningCert.pfx', 'secret');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  // Encrypt with AES-256 and do not permit modifying.
  pdf.userPassword = 'user-secret';
  pdf.ownerPassword = 'owner-secret';
  final json = CkJsonObject();
  json.updateBool('permissions.modify', false);
  final jsonOut = CkJsonObject();
  try {
    pdf.protection('encrypt', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The encryption is still pending.  SignPdf applies it first and signs the encrypted result.
  try {
    pdf.setSigningCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sigOpts = CkJsonObject();
  sigOpts.updateBool('invisibleSignature', true);
  sigOpts.updateInt('signingCertificateV2', 1);
  sigOpts.updateInt('signingTime', 1);
  try {
    pdf.signPdf(sigOpts, 'qa_output/memo_encrypted_signed.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // The result is encrypted and signed.
  final check = CkPdf();
  check.userPassword = 'user-secret';
  try {
    check.loadFile('qa_output/memo_encrypted_signed.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  print('algorithm: ${info.stringOf('loaded.algorithm')}');
  print('signatures: ${check.numSignatures}');
  final sigInfo = CkJsonObject();
  var verified = true;
  try {
    check.verifySignature(0, sigInfo);
  } on ChilkatException {
    verified = false;
  }
  print('signature verifies: $verified');

  // Signing an encrypted PDF.  This one was opened with only the user password, and its
  // permissions do not allow modifying, so another signature is refused.
  try {
    check.setSigningCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    check.signPdf(sigOpts, 'qa_output/memo_signed_twice.pdf');
  } on ChilkatException {
    print('A second signature was refused: the permissions do not allow it.');
  }

  // Loaded with the owner password, the same PDF can be signed.  It stays encrypted, and the
  // first signature stays valid because the new one is added as an incremental update.
  final owner = CkPdf();
  owner.userPassword = 'user-secret';
  owner.ownerPassword = 'owner-secret';
  try {
    owner.loadFile('qa_output/memo_encrypted_signed.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    owner.setSigningCert(cert);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    owner.signPdf(sigOpts, 'qa_output/memo_signed_twice.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final twice = CkPdf();
  twice.userPassword = 'user-secret';
  try {
    twice.loadFile('qa_output/memo_signed_twice.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('signatures after signing with owner rights: ${twice.numSignatures}');
  verified = true;
  try {
    twice.verifySignature(0, sigInfo);
  } on ChilkatException {
    verified = false;
  }
  print('first signature still verifies: $verified');
  verified = true;
  try {
    twice.verifySignature(1, sigInfo);
  } on ChilkatException {
    verified = false;
  }
  print('second signature verifies: $verified');

  // Sample output:

  // algorithm: aes-256
  // signatures: 1
  // signature verifies: True
  // A second signature was refused: the permissions do not allow it.
  // signatures after signing with owner rights: 2
  // first signature still verifies: True
  // second signature verifies: True
}