Skip to content
Guides

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

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:

  1. Set distinct user passwords and owner passwords.

  2. Create a permissions information class.

  3. Specify the encryption algorithm.

  4. Encrypt the document using the user password, owner password, permission information, and the chosen algorithm.

This sample shows how to encrypt a document:

swift
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)

let surl = URL(fileURLWithPath: "")
let options: [CPDFDocumentWriteOption: Any] = [
      .ownerPasswordOption: "userPassword",
      .userPasswordOption: "ownerPassword",
  		.encryptionLevelOption: CPDFDocumentEncryptionLevel.AES128,
      .allowsPrintingOption: true,
      .allowsCopyingOption: false
  ]

document?.write(to: surl, withOptions: options)
objective-c
NSURL *url = [NSURL fileURLWithPath:@"File Path"];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

NSURL *surl = [NSURL fileURLWithPath:@""];
NSDictionary *options = @{CPDFDocumentOwnerPasswordOption : @"userPassword",
                          CPDFDocumentUserPasswordOption : @"ownerPassword",
                          CPDFDocumentEncryptionLevelOption : @(CPDFDocumentEncryptionLevelAES256),
                          CPDFDocumentAllowsPrintingOption : @(YES),
                          CPDFDocumentAllowsCopyingOption : @(NO),
                          };
[document writeToURL:surl withOptions:options];

Encryption Algorithm and its Description:

AlgorithmDescriptionEnumeration Values
NoneNo encryptionCPDFDocumentEncryptionLevel.CPDFDocumentNoEncryptAlgo
RC4Encrypts plaintext using XOR with keyCPDFDocumentEncryptionLevel.CPDFDocumentRC4
AES-128Encrypts using AES algorithm with 128-bit keyCPDFDocumentEncryptionLevel.CPDFDocumentAES128
AES-256Encrypts using AES algorithm with 256-bit keyCPDFDocumentEncryptionLevel.CPDFDocumentAES256

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:

  1. Retrieve document permission information.

  2. Use the document permission information to view specific permissions.

This example shows how to view document permissions:

swift
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)

var commandLineStr = ""
commandLineStr.append("Printing: \(document!.allowsPrinting ? "true" : "false")\n")
commandLineStr.append("Content Copying: \(document!.allowsCopying ? "true" : "false")\n")
objective-c
NSURL *url = [NSURL fileURLWithPath:@"File Path"];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

NSString *commandLineStr = "";
[commandLineStr stringByAppendingFormat:@"Printing: %@\n", document.allowsPrinting ? @"true" : @"flase"];
commandLineStr = [commandLineStr stringByAppendingFormat:@"Content Copying:: %@\n", document.allowsCopying ? @"true" : @"flase"];

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:

  1. When opening the document, check if it is encrypted.

  2. 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:

swift
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)

if document?.isLocked == true {
    document?.unlock(withPassword: "password")
}
objective-c
NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

if (document.isLocked) {
		[document unlockWithPassword:@"password"];
}

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:

  1. Unlock the document to obtain all permissions.

  2. Save the unlocked document.

This example shows how to remove passwords:

objective-c
let url = URL(fileURLWithPath: "File Path")
let document = CPDFDocument(url: url)

if document?.isLocked == true {
  	let surl = URL(fileURLWithPath: "")
    document?.unlock(withPassword: "password")
    document?.writeDecrypt(to: surl)
}
objective-c
NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

if (document.isLocked) {
  	NSURL *surl = [NSURL fileURLWithPath:@""];
		[document unlockWithPassword:@"password"];
  	[document writeDecryptToURL:surl];
}