iOS
ComPDFKit PDF SDK
Guides

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, CPDFView 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::unlockWithPassword: 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.

 

/**
 * A Boolean value indicating whether the document allows printing.
 */
@property (nonatomic,readonly) BOOL allowsPrinting;
/**
 * A Boolean value indicating whether the document allows printing in high fidelity.
 */
@property (nonatomic,readonly) BOOL allowsHighQualityPrinting;
/**
 * A Boolean value indicating whether the document allows copying of content to the Pasteboard.
 */
@property (nonatomic,readonly) BOOL allowsCopying;
/**
 * A Boolean value indicating whether you can modify the document contents except for document attributes.
 */
@property (nonatomic,readonly) BOOL allowsDocumentChanges;
/**
 * A Boolean value indicating whether you can manage a document by inserting, deleting, and rotating pages.
 */
@property (nonatomic,readonly) BOOL allowsDocumentAssembly;
/**
 * A Boolean value indicating whether you can create or modify document annotations, including form field entries.
 */
@property (nonatomic,readonly) BOOL allowsCommenting;
/**
 * A Boolean value indicating whether you can modify form field entries even if you can't edit document annotations.
 */
@property (nonatomic,readonly) BOOL 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.

 

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.

 

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
​
NSURL *surl = [NSURL fileURLWithPath:@""];
NSDictionary *options = @{CPDFDocumentOwnerPasswordOption : @"The owner password",
                          CPDFDocumentUserPasswordOption : @"The user password",
                          CPDFDocumentAllowsPrintingOption : @(YES),
                          CPDFDocumentAllowsHighQualityPrintingOption : @(NO),
                          CPDFDocumentAllowsCopyingOption : @(NO),
                          CPDFDocumentAllowsDocumentChangesOption : @(NO),
                          CPDFDocumentAllowsDocumentAssemblyOption : @(NO),
                          CPDFDocumentAllowsCommentingOption : @(NO),
                          CPDFDocumentAllowsFormFieldEntryOption : @(NO)};
[document writeToURL:surl withOptions:options];

 

Decrypt

 

ComPDFKit PDF SDK fully supports the reading of secured and encrypted PDF documents.

 

- To check whether a document requires a password:

 

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
​
if (document.error && 
    document.error.code == CPDFDocumentPasswordError) {
    // Password required
}

 

- To read a PDF document with password protection, use function CPDFDocument::unlockWithPassword:. If the password is correct, this method returns YES, a CPDFDocumentDidUnlockNotification notification is sent. Once unlocked, you cannot use this function to relock the document. Once unlocked, you cannot use this function to relock the document.

 

- To remove PDF security, call the CPDFDocument::writeDecryptToURL: method:

 

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];

NSURL *surl = [NSURL fileURLWithPath:@""];
[document writeDecryptToURL:surl];