Security
ComPDFKit PDF SDK protects the content of PDF documents from unauthorized access like copying or printing. It offers developers a way to encrypt and decrypt PDFs, add a password, insert watermark, and more. For controlling over document security in ComPDFKit PDF SDK, security handlers perform user authorization and sets various permissions over PDF documents.
PDF Permission
A PDF file can have two different passwords set, a permissions or owner password and an open or user password.
A user password (also known as an open password) requires a user to type a password to open the PDF. When you try to open a document with a user password, CPDFDocument.isEncrypted()
will show a password prompt to unlock the document. If you want to open a document with a user password programmatically, you can use the CPDFDocument.open(String absolutePath, String password)
API.
An owner password (also known as a permissions password) requires a password to change permission settings. When an owner password is set, you can configure a set of permissions. For example, you can configure an owner password and the “printing” permission when saving a document to make sure that users who don’t know that owner password can only print the document, but not modify it.
The PDF specification defines the permissions shown below:
- Printing — print the document.
- High-quality printing — print the document in high fidelity.
- Copying — copy content from the document.
- Document changes — modify the document contents except for document attributes.
- Document assembly — insert, delete, and rotate pages.
- Commenting — create or modify document annotations, including form field entries.
- Form Field Entry — modify form field entries even if you can't edit document annotations.
To access the corresponding permissions, refer to following methods in the CPDFDocument
class.
public CPDFDocumentPermissionInfo getPermissionsInfo() {
//...
}
public class CPDFDocumentPermissionInfo {
/****** Printing the document ******/
private boolean allowsPrinting;
/****** The document allows printing in high fidelity ******/
private boolean allowsHighQualityPrinting;
/****** Extract content (text, images, etc.) ******/
private boolean allowsCopying;
/****** Modify the document contents except for page management (document attributes) ******/
private boolean allowsDocumentChanges;
/****** Page management: insert, delete, and rotate pages ******/
private boolean allowsDocumentAssembly;
/****** Create or modify annotations, including form field entries ******/
private boolean allowsCommenting;
/****** Modify form field entries, even if allowsCommenting is T_FALSE ******/
private boolean allowsFormFieldEntry;
}
Encrypt
ComPDFKit’s CPDFDocument
API can generate a password-protected document. You can use CPDFDocument
to create a new password-protected PDF document on disk based on a current document. The user password prevents users from viewing the PDF. If you specify it, you also need to specify an owner password.
How to create a password-protected document:
// Open document from file path
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
//set user password
document.setUserPassword(password);
//set owner password
document.setOwnerPassword(password);
//save document
document.save();
Decrypt
ComPDFKit fully supports the reading of secured and encrypted PDF documents.
- To check whether a document requires a password:
// Open document from file path
CPDFDocument document = new CPDFDocument(context);
CPDFDocument.PDFDocumentError error = document.open(pdfPath, passwod);
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
// Password required
}
- To read a PDF document with password protection, use function CPDFDocument.isEncrypted()
. If the password is correct, this method returns true
. Once unlocked, you cannot use this function to relock the document.
- To remove PDF security, call the CPDFDocument.save()
method:
// Open document from file path
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath, passwod);
//remove password
try {
document.save(CPDFDocument.PDFDocumentSaveType.PDFDocumentSaveRemoveSecurity);
} catch (CPDFDocumentException e) {
e.printStackTrace();
}