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:
- Set distinct user passwords and owner passwords.
- Create a permissions information class.
- Specify the encryption algorithm.
- Encrypt the document using the user password, owner password, permission information, and the chosen algorithm.
- Save the document
This sample shows how to encrypt a document:
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:
Algorithm | Description | Enum Value |
---|---|---|
No Encrypt Algo | No encryption | CPDFDocumentEncryptAlgo.noEncryptAlgo |
RC4 | Encrypts plain text using key-based XOR | CPDFDocumentEncryptAlgo.rc4 |
AES-128 | AES encryption using a 128-bit key | CPDFDocumentEncryptAlgo.aes128 |
AES-256 | AES encryption using a 256-bit key | CPDFDocumentEncryptAlgo.aes256 |