Objective-C Requires Chilkat v11.7.0+
Objective-C
Restrict Printing, Copying and Editing of a PDF
See more PDF Protection Examples
Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.Chilkat Objective-C Downloads
#import <CkoPdf.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.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 two_page_memo.pdf
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/two_page_memo.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// No user password: the PDF opens without a prompt. The owner password is what protects the
// restrictions, so it must not be empty when anything is restricted.
pdf.UserPassword = @"";
pdf.OwnerPassword = @"owner-secret";
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"permissions.print" value: @"lowQuality"];
[json UpdateBool: @"permissions.copy" value: NO];
[json UpdateBool: @"permissions.modify" value: NO];
[json UpdateBool: @"permissions.annotate" value: NO];
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Protection: @"encrypt" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
success = [pdf WriteFile: @"qa_output/memo_restricted.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// Anyone can open the result, with the rights of a user.
CkoPdf *reader = [[CkoPdf alloc] init];
success = [reader LoadFile: @"qa_output/memo_restricted.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(@"%@%@",@"opened with: ",[info StringOf: @"loaded.openedWith"]);
NSLog(@"%@%@",@"print: ",[info StringOf: @"loaded.permissions.print"]);
NSLog(@"%@%d",@"copy: ",[info BoolOf: @"loaded.permissions.copy"]);
NSLog(@"%@%d",@"modify: ",[info BoolOf: @"loaded.permissions.modify"]);
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
CkoJsonObject *opts = [[CkoJsonObject alloc] init];
CkoJsonObject *extractOut = [[CkoJsonObject alloc] init];
CkoStringBuilder *sb = [[CkoStringBuilder alloc] init];
success = [reader ExtractTextSb: @"text" jsonIn: opts sbOut: sb jsonOut: extractOut];
if (success == NO) {
NSLog(@"%@",@"Text extraction was refused: the PDF does not permit copying.");
}
// The owner password lifts every restriction.
CkoPdf *owner = [[CkoPdf alloc] init];
owner.OwnerPassword = @"owner-secret";
success = [owner LoadFile: @"qa_output/memo_restricted.pdf"];
if (success == NO) {
NSLog(@"%@",owner.LastErrorText);
return;
}
CkoJsonObject *ownerInfo = [[CkoJsonObject alloc] init];
success = [owner Protection: @"getInfo" jsonIn: empty jsonOut: ownerInfo];
if (success == NO) {
NSLog(@"%@",owner.LastErrorText);
return;
}
NSLog(@"%@%@",@"opened with: ",[ownerInfo StringOf: @"loaded.openedWith"]);
success = [owner ExtractTextSb: @"text" jsonIn: opts sbOut: sb jsonOut: extractOut];
if (success == NO) {
NSLog(@"%@",owner.LastErrorText);
return;
}
NSLog(@"%@%d",@"characters extracted with the owner password: ",[sb.Length intValue]);
// Sample output:
// opened with: emptyPassword
// print: lowQuality
// copy: False
// modify: False
// Text extraction was refused: the PDF does not permit copying.
// opened with: owner
// characters extracted with the owner password: 1048