PDF Permissions
PDF Permissions are employed to ensure the security of PDF documents, offering encryption, document permissions, decryption, and password removal features. This ensures users have secure control and effective management over the document.
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:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
// Set user password.
document.setUserPassword(user_password);
// Set encrypt algorithm type.
document.setEncryptAlgorithm(PDFDocumentEncryptAlgo.PDFDocumentAES256);
// Set owner password.
document.setOwnerPassword(owner_password);
// Set permission info.
CPDFDocumentPermissionInfo info = document.getPermissionsInfo();
info.setAllowsPrinting(false);
info.setAllowsCopying(true);
document.setPermissionsInfo(info);
// Save document.
document.save();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
val document = CPDFDocument(context).apply {
// Open document from file path.
open(pdfPath)
// Set user password.
setUserPassword(user_password)
// Set encrypt algorithm type.
encryptAlgorithm = PDFDocumentEncryptAlgo.PDFDocumentAES256
// Set owner password.
setOwnerPassword(owner_password)
// Set permission info.
permissionsInfo.apply {
isAllowsPrinting = false
isAllowsCopying = true
}
save()
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Encryption Algorithm and its Description:
Algorithm | Description | Enumeration Values |
---|---|---|
None | No encryption | CPDFDocument.PDFDocumentEncryptAlgo.PDFDocumentNoEncryptAlgo |
RC4 | Encrypts plaintext using XOR with key | CPDFDocument.PDFDocumentEncryptAlgo.PDFDocumentRC4 |
AES-128 | Encrypts using AES algorithm with 128-bit key | CPDFDocument.PDFDocumentEncryptAlgo.PDFDocumentAES128 |
AES-256 | Encrypts using AES algorithm with 256-bit key | CPDFDocument.PDFDocumentEncryptAlgo.PDFDocumentAES256 |
PDF Permissions
In the PDF specification, there is support for configuring various permissions for a document. By configuring these permissions, it is possible to restrict users to perform only the expected actions.
The PDF specification defines the following permissions:
- Print: Allows printing of the document.
- High-Quality Print: Permits high-fidelity printing of the document.
- Copy: Enables copying of the document content.
- Modify Document: Allows modification of the document content, excluding document properties.
- Assemble Document: Permits insertion, deletion, and rotation of pages.
- Annotate: Enables the creation or modification of document annotations, including form field entries.
- Form Field Input: Allows modification of form field entries, even if document annotations are not editable.
The steps to view document permissions are as follows:
Retrieve document permission information.
Use the document permission information to view specific permissions.
This example shows how to view document permissions:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
// get permission info.
CPDFDocumentPermissionInfo info = document.getPermissionsInfo();
System.out.println("Allows Printing:" + info.isAllowsPrinting());
System.out.println("Allows Copying:" + info.isAllowsCopying());
2
3
4
5
6
7
// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath)
// get permission info.
val info = document.getPermissionsInfo()
println("Allows Printing:${info.isAllowsPrinting}")
println("Allows Copying:${info.isAllowsCopying}")
2
3
4
5
6
7
Decrypt
Accessing a password-protected PDF document requires entering the password. Different levels of passwords provide varying levels of permission.
The steps for decryption are as follows:
When opening the document, check if it is encrypted.
For encrypted documents, entering either the user password or the owner password allows the document to be opened.
This example shows how to decrypt a document:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
CPDFDocument.PDFDocumentError error = document.open(pdfPath);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// Password required.
document.open(pdfFath, password);
}
2
3
4
5
6
7
// Open document from file path.
val document = CPDFDocument(context)
val error: PDFDocumentError = document.open(pdfPath)
if (error == PDFDocumentError.PDFDocumentErrorPassword) {
// Password required.
document.open(pdfPath, password)
}
2
3
4
5
6
7
Remove passwords
Removing passwords means deleting the owner passwords and user passwords from a document and saving it as a new document, which will no longer require a password to open and will have all permissions available by default.
The steps to remove passwords are as follows:
- Unlock the document to obtain all permissions.
- Save the unlocked document.
This example shows how to remove passwords:
// Open document from file path.
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, password);
// Remove password.
try {
document.save(CPDFDocument.PDFDocumentSaveType.PDFDocumentSaveRemoveSecurity);
} catch (CPDFDocumentException e) {
e.printStackTrace();
}
2
3
4
5
6
7
8
9
// Open document from file path.
val document = CPDFDocument(context)
document.open(pdfPath, password)
// Remove password.
try {
document.save(CPDFDocument.PDFDocumentSaveType.PDFDocumentSaveRemoveSecurity)
} catch (e: CPDFDocumentException) {
e.printStackTrace()
}
2
3
4
5
6
7
8
9