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

Encrypt and Sign a PDF, and Sign an Encrypted PDF

See more PDF Protection Examples

Demonstrates signing together with encryption. A pending encrypt is applied by SignPdf before it signs, so one call produces a PDF that is both encrypted and signed. A PDF that is already encrypted can be signed too, and stays encrypted. When it was opened with only the user password its permissions decide: adding a signature field needs both the modify and the annotate permission. Loading the PDF with its owner password lifts that.

Chilkat Objective-C Downloads

Objective-C
#import <CkoCert.h>
#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 two_page_memo.pdf

//  The signing certificate, with its private key.
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/myPdfSigningCert.pfx" password: @"secret"];
if (success == NO) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/two_page_memo.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  Encrypt with AES-256 and do not permit modifying.
pdf.UserPassword = @"user-secret";
pdf.OwnerPassword = @"owner-secret";
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateBool: @"permissions.modify" value: NO];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Protection: @"encrypt" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  The encryption is still pending.  SignPdf applies it first and signs the encrypted result.
success = [pdf SetSigningCert: cert];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

CkoJsonObject *sigOpts = [[CkoJsonObject alloc] init];
[sigOpts UpdateBool: @"invisibleSignature" value: YES];
[sigOpts UpdateInt: @"signingCertificateV2" value: [NSNumber numberWithInt: 1]];
[sigOpts UpdateInt: @"signingTime" value: [NSNumber numberWithInt: 1]];
success = [pdf SignPdf: sigOpts outFilePath: @"qa_output/memo_encrypted_signed.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf.LastErrorText);
    return;
}

//  The result is encrypted and signed.
CkoPdf *check = [[CkoPdf alloc] init];
check.UserPassword = @"user-secret";
success = [check LoadFile: @"qa_output/memo_encrypted_signed.pdf"];
if (success == NO) {
    NSLog(@"%@",check.LastErrorText);
    return;
}

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

NSLog(@"%@%@",@"algorithm: ",[info StringOf: @"loaded.algorithm"]);
NSLog(@"%@%d",@"signatures: ",[check.NumSignatures intValue]);
CkoJsonObject *sigInfo = [[CkoJsonObject alloc] init];
BOOL verified = [check VerifySignature: [NSNumber numberWithInt: 0] sigInfo: sigInfo];
NSLog(@"%@%d",@"signature verifies: ",verified);

//  Signing an encrypted PDF.  This one was opened with only the user password, and its
//  permissions do not allow modifying, so another signature is refused.
success = [check SetSigningCert: cert];
if (success == NO) {
    NSLog(@"%@",check.LastErrorText);
    return;
}

success = [check SignPdf: sigOpts outFilePath: @"qa_output/memo_signed_twice.pdf"];
if (success == NO) {
    NSLog(@"%@",@"A second signature was refused: the permissions do not allow it.");
}

//  Loaded with the owner password, the same PDF can be signed.  It stays encrypted, and the
//  first signature stays valid because the new one is added as an incremental update.
CkoPdf *owner = [[CkoPdf alloc] init];
owner.UserPassword = @"user-secret";
owner.OwnerPassword = @"owner-secret";
success = [owner LoadFile: @"qa_output/memo_encrypted_signed.pdf"];
if (success == NO) {
    NSLog(@"%@",owner.LastErrorText);
    return;
}

success = [owner SetSigningCert: cert];
if (success == NO) {
    NSLog(@"%@",owner.LastErrorText);
    return;
}

success = [owner SignPdf: sigOpts outFilePath: @"qa_output/memo_signed_twice.pdf"];
if (success == NO) {
    NSLog(@"%@",owner.LastErrorText);
    return;
}

CkoPdf *twice = [[CkoPdf alloc] init];
twice.UserPassword = @"user-secret";
success = [twice LoadFile: @"qa_output/memo_signed_twice.pdf"];
if (success == NO) {
    NSLog(@"%@",twice.LastErrorText);
    return;
}

NSLog(@"%@%d",@"signatures after signing with owner rights: ",[twice.NumSignatures intValue]);
verified = [twice VerifySignature: [NSNumber numberWithInt: 0] sigInfo: sigInfo];
NSLog(@"%@%d",@"first signature still verifies: ",verified);
verified = [twice VerifySignature: [NSNumber numberWithInt: 1] sigInfo: sigInfo];
NSLog(@"%@%d",@"second signature verifies: ",verified);

//  Sample output:

//  algorithm: aes-256
//  signatures: 1
//  signature verifies: True
//  A second signature was refused: the permissions do not allow it.
//  signatures after signing with owner rights: 2
//  first signature still verifies: True
//  second signature verifies: True