Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.7.0+

Change the Permissions of an Encrypted PDF

See more PDF Protection Examples

Demonstrates the changePermissions operation, which changes what an encrypted PDF permits and keeps its passwords, algorithm and scope. The JSON takes the same permissions member as the encrypt operation, and a permission that is not mentioned is granted. The operation needs owner rights. For an AES-256 PDF, load with both passwords, because the user password cannot be recovered from the owner password alone.

Chilkat Objective-C Downloads

Objective-C
#import <CkoPdf.h>
#import <CkoJsonObject.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  The PDF used in this example is available at encrypted_user_password.pdf

CkoPdf *pdf = [[CkoPdf alloc] init];

//  Changing the permissions needs owner rights.  Both passwords are given here, which is what
//  an AES-256 PDF requires for this operation.
pdf.UserPassword = @"secret";
pdf.OwnerPassword = @"owner-secret";
success = [pdf LoadFile: @"qa_data/pdf/encrypted_user_password.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  No printing and no copying.  Everything not mentioned is granted.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"permissions.print" value: @"none"];
[json UpdateBool: @"permissions.copy" value: NO];

CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Protection: @"changePermissions" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

NSLog(@"%@%@",@"print: ",[jsonOut StringOf: @"pending.permissions.print"]);
NSLog(@"%@%d",@"copy: ",[jsonOut BoolOf: @"pending.permissions.copy"]);
NSLog(@"%@%d",@"modify: ",[jsonOut BoolOf: @"pending.permissions.modify"]);
NSLog(@"%@%d",@"password still needed to open: ",[jsonOut BoolOf: @"pending.userPasswordRequired"]);

success = [pdf WriteFile: @"qa_output/minutes_no_print.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  The same user password opens the result, now with the new permissions.
CkoPdf *reader = [[CkoPdf alloc] init];
reader.UserPassword = @"secret";
success = [reader LoadFile: @"qa_output/minutes_no_print.pdf"];
if (success == NO) {
    NSLog(@"%@",reader.LastErrorText);
    return;
}

CkoJsonObject *empty = [[CkoJsonObject alloc] init];
CkoJsonObject *info = [[CkoJsonObject alloc] init];
success = [reader Protection: @"getInfo" jsonIn: empty jsonOut: info];
if (success == NO) {
    NSLog(@"%@",reader.LastErrorText);
    return;
}

NSLog(@"%@%@",@"saved file, print: ",[info StringOf: @"loaded.permissions.print"]);
NSLog(@"%@%d",@"saved file, copy: ",[info BoolOf: @"loaded.permissions.copy"]);

//  Sample output:

//  print: none
//  copy: False
//  modify: True
//  password still needed to open: True
//  saved file, print: none
//  saved file, copy: False