Guides
Set Password
The PDF permission module ensures document security by providing encryption, document permissions, decryption, and password removal functions. It allows users to securely control and manage the document.
Encryption
Encryption consists of two parts: user password and owner password.
- User Password is used to open the document, ensuring that only authorized users can access the content. When a user password is set, certain document permissions such as editing, copying, or printing are usually restricted.
- Owner Password allows not only to open the document but also to unlock all restricted permissions, enabling the user to edit, copy, or print the document.
This dual-password system is designed to offer more flexible and secure document access and management.
ComPDFKit provides various encryption algorithms and permission settings. Based on your needs, you can choose the appropriate algorithm and configure custom permissions to protect the document.
The encryption steps are as follows:
- Set different user passwords and owner passwords.
- Create permission information.
- Specify the encryption algorithm.
- Encrypt the document using the user password, owner password, permission information, and specified algorithm.
- Save the document.
The following example demonstrates how to encrypt a document:
tsx
const CPDFReaderViewExampleScreen = () => {
const pdfReaderRef = useRef<CPDFReaderView>(null);
const [samplePDF] = useState(
(Platform.OS === 'android'
? 'file:///android_asset/PDF_Document.pdf'
: 'PDF_Document.pdf')
);
return (
<View style={{ flex: 1 }}>
<Button title='Set Password' onPress={async () => {
var document = pdfReaderRef.current?._pdfDocument;
const allowsPrinting = false;
const allowsCopy = false;
const result = await document?.setPassword(
'1234', // Document opening password
'4321', // Owner password
allowsPrinting,
allowsCopy,
CPDFDocumentEncryptAlgo.AES128);
console.log('ComPDFKit-RN setPassword:', result);
}}></Button>
<CPDFReaderView
ref={pdfReaderRef}
document={samplePDF}
configuration={ComPDFKit.getDefaultConfig({
toolbarConfig: {
mainToolbarVisible: true,
iosLeftBarAvailableActions: [
CPDFToolbarAction.THUMBNAIL
]
}
})}
/>
</View>
);
};
Different Encryption Algorithms
Encryption Algorithm | Description | Enum Value |
---|---|---|
No Encrypt Algo | No encryption | CPDFDocumentEncryptAlgo.noEncryptAlgo |
RC4 | Key-based XOR encryption of plaintext | CPDFDocumentEncryptAlgo.rc4 |
AES-128 | AES encryption with a 128-bit key | CPDFDocumentEncryptAlgo.aes128 |
AES-256 | AES encryption with a 256-bit key | CPDFDocumentEncryptAlgo.aes256 |