Objective-C Requires Chilkat v11.7.0+
Objective-C
Watermark a Signed PDF Without Breaking the Signature
See more PDF Watermark Examples
Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.Chilkat Objective-C Downloads
#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 invoice_signed.pdf
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/invoice_signed.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"signatures: ",[pdf.NumSignatures intValue]);
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
[json UpdateString: @"type" value: @"text"];
[json UpdateString: @"text" value: @"PAID"];
[json UpdateString: @"id" value: @"paid"];
[json UpdateString: @"color" value: @"#2E7D32"];
[json UpdateString: @"position.align" value: @"topRight"];
[json UpdateNumber: @"position.margin" numericStr: @"36"];
// An artifact watermark changes the page content, so it is refused while the signatures matter.
[json UpdateString: @"approach" value: @"artifact"];
success = [pdf Watermark: @"add" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",@"The artifact approach was refused on a signed PDF, as it should be.");
}
// An annotation is added without touching the signed bytes.
[json UpdateString: @"approach" value: @"annotation"];
success = [pdf Watermark: @"add" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%@",@"annotation added, pages: ",[jsonOut StringOf: @"pages"]);
success = [pdf WriteFile: @"qa_output/invoice_paid.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// The signature still verifies: the original bytes are unchanged and the annotation was appended.
CkoPdf *check = [[CkoPdf alloc] init];
success = [check LoadFile: @"qa_output/invoice_paid.pdf"];
if (success == NO) {
NSLog(@"%@",check.LastErrorText);
return;
}
CkoJsonObject *sigInfo = [[CkoJsonObject alloc] init];
BOOL verified = [check VerifySignature: [NSNumber numberWithInt: 0] sigInfo: sigInfo];
NSLog(@"%@%d",@"signature still verifies: ",verified);
// Sample output:
// signatures: 1
// The artifact approach was refused on a signed PDF, as it should be.
// annotation added, pages: 1
// signature still verifies: True