Skip to content
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:

  1. Set different user passwords and owner passwords.
  2. Create permission information.
  3. Specify the encryption algorithm.
  4. Encrypt the document using the user password, owner password, permission information, and specified algorithm.
  5. 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 AlgorithmDescriptionEnum Value
No Encrypt AlgoNo encryptionCPDFDocumentEncryptAlgo.noEncryptAlgo
RC4Key-based XOR encryption of plaintextCPDFDocumentEncryptAlgo.rc4
AES-128AES encryption with a 128-bit keyCPDFDocumentEncryptAlgo.aes128
AES-256AES encryption with a 256-bit keyCPDFDocumentEncryptAlgo.aes256