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

React Native
import { Cert, JsonObject, Pdf } from '@chilkat/react-native'

async function chilkatExample() {
  // 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.
  const cert = new Cert();
  try {
    cert.loadPfxFile('qa_data/pfx/myPdfSigningCert.pfx', 'secret');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  const pdf = new Pdf();
  try {
    pdf.loadFile('qa_data/pdf/two_page_memo.pdf');
  } catch {
    console.log(pdf.lastErrorText);
    return;
  }

  // Encrypt with AES-256 and do not permit modifying.
  pdf.userPassword = 'user-secret';
  pdf.ownerPassword = 'owner-secret';
  const json = new JsonObject();
  json.updateBool('permissions.modify', false);
  const jsonOut = new JsonObject();
  try {
    pdf.protection('encrypt', json, jsonOut);
  } catch {
    console.log(pdf.lastErrorText);
    return;
  }

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

  const sigOpts = new JsonObject();
  sigOpts.updateBool('invisibleSignature', true);
  sigOpts.updateInt('signingCertificateV2', 1);
  sigOpts.updateInt('signingTime', 1);
  try {
    await pdf.signPdfAsync(sigOpts, 'qa_output/memo_encrypted_signed.pdf');
  } catch {
    console.log(pdf.lastErrorText);
    return;
  }

  // The result is encrypted and signed.
  const check = new Pdf();
  check.userPassword = 'user-secret';
  try {
    check.loadFile('qa_output/memo_encrypted_signed.pdf');
  } catch {
    console.log(check.lastErrorText);
    return;
  }

  const empty = new JsonObject();
  const info = new JsonObject();
  try {
    check.protection('getInfo', empty, info);
  } catch {
    console.log(check.lastErrorText);
    return;
  }

  console.log(`algorithm: ${info.stringOf('loaded.algorithm')}`);
  console.log(`signatures: ${check.numSignatures}`);
  const sigInfo = new JsonObject();
  let verified = true;
  try {
    check.verifySignature(0, sigInfo);
  } catch {
    verified = false;
  }
  console.log(`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);
  } catch {
    console.log(check.lastErrorText);
    return;
  }

  try {
    await check.signPdfAsync(sigOpts, 'qa_output/memo_signed_twice.pdf');
  } catch {
    console.log('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.
  const owner = new Pdf();
  owner.userPassword = 'user-secret';
  owner.ownerPassword = 'owner-secret';
  try {
    owner.loadFile('qa_output/memo_encrypted_signed.pdf');
  } catch {
    console.log(owner.lastErrorText);
    return;
  }

  try {
    owner.setSigningCert(cert);
  } catch {
    console.log(owner.lastErrorText);
    return;
  }

  try {
    await owner.signPdfAsync(sigOpts, 'qa_output/memo_signed_twice.pdf');
  } catch {
    console.log(owner.lastErrorText);
    return;
  }

  const twice = new Pdf();
  twice.userPassword = 'user-secret';
  try {
    twice.loadFile('qa_output/memo_signed_twice.pdf');
  } catch {
    console.log(twice.lastErrorText);
    return;
  }

  console.log(`signatures after signing with owner rights: ${twice.numSignatures}`);
  verified = true;
  try {
    twice.verifySignature(0, sigInfo);
  } catch {
    verified = false;
  }
  console.log(`first signature still verifies: ${verified}`);
  verified = true;
  try {
    twice.verifySignature(1, sigInfo);
  } catch {
    verified = false;
  }
  console.log(`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
}