Skip to content

Setting Password

The PDF permission module ensures document security by providing encryption, document permissions, decryption, and password removal functionalities, allowing users to securely control and manage documents.

Encrypt

Encrypt function consists of two parts: User Password and Owner Password.

The User Password is utilized to open the document, ensuring that only authorized users can access its content. When a user password is set, it typically restricts certain document permissions such as modification, copying, or printing. On the other hand, the Owner Password not only opens the document but also unlocks all restricted permissions, allowing users to modify, copy, or print the document. The dual-password system aims to provide a more flexible and secure approach to document access and management.

ComPDFKit offers a variety of encryption algorithms and permission settings. Depending on your requirements, you can use the appropriate algorithm and configure custom permissions to safeguard your document.

The steps to encrypt are as follows:

  1. Set distinct user passwords and owner passwords.
  2. Create a permissions information class.
  3. Specify the encryption algorithm.
  4. Encrypt the document using the user password, owner password, permission information, and the chosen algorithm.
  5. Save the document

This sample shows how to encrypt a document:

dart
CPDFReaderWidgetController? _controller;

@override
Widget build(BuildContext context) {
  return Scaffold(
    resizeToAvoidBottomInset: false,
    appBar: AppBar(),
    body: Column(children: [
      TextButton(onPressed: () async{
        // set password
        bool setPasswordResult = await _controller.document.setPassword(
          userPassword: '1234',
          ownerPassword: '12345',
          allowsPrinting: false,
          allowsCopying: false,
          encryptAlgo: CPDFDocumentEncryptAlgo.aes256);
        debugPrint('ComPDFKit-Flutter: setPasswordResult:$setPasswordResult');
        // save pdf
        await _controller.document.save();
      }, child: const Text('Encrypt PDF')),
      Expanded(child: CPDFReaderWidget(
        document: widget.documentPath,
        configuration: CPDFConfiguration(),
        onCreated: (controller) {
          setState(() {
            _controller = controller;
          });
        },
      ))
    ],));
}

Different Encryption Algorithms:

AlgorithmDescriptionEnum Value
No Encrypt AlgoNo encryptionCPDFDocumentEncryptAlgo.noEncryptAlgo
RC4Encrypts plain text using key-based XORCPDFDocumentEncryptAlgo.rc4
AES-128AES encryption using a 128-bit keyCPDFDocumentEncryptAlgo.aes128
AES-256AES encryption using a 256-bit keyCPDFDocumentEncryptAlgo.aes256