Objective-C Requires Chilkat v11.7.0+
Objective-C
Watermark Only Some of a PDF's Pages
See more PDF Watermark Examples
Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.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 four_pages.pdf
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/four_pages.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"pages in the document: ",[pdf.NumPages intValue]);
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
[json UpdateString: @"type" value: @"text"];
[json UpdateString: @"text" value: @"INTERNAL"];
[json UpdateString: @"id" value: @"internal"];
// Every page except the first.
[json UpdateString: @"pages" value: @"all"];
[json UpdateString: @"excludePages" value: @"1"];
success = [pdf Watermark: @"add" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%@",@"every page but the cover: ",[jsonOut StringOf: @"pages"]);
// A list of pages and ranges, on a second copy of the document.
CkoPdf *pdf2 = [[CkoPdf alloc] init];
success = [pdf2 LoadFile: @"qa_data/pdf/four_pages.pdf"];
if (success == NO) {
NSLog(@"%@",pdf2.LastErrorText);
return;
}
CkoJsonObject *json2 = [[CkoJsonObject alloc] init];
[json2 UpdateString: @"type" value: @"text"];
[json2 UpdateString: @"text" value: @"APPENDIX"];
[json2 UpdateString: @"id" value: @"appendix"];
[json2 UpdateString: @"pages" value: @"2,4-"];
success = [pdf2 Watermark: @"add" jsonIn: json2 jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf2.LastErrorText);
return;
}
NSLog(@"%@%@",@"page 2 and 4 onwards: ",[jsonOut StringOf: @"pages"]);
success = [pdf WriteFile: @"qa_output/four_pages_internal.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// Sample output:
// pages in the document: 4
// every page but the cover: 2-4
// page 2 and 4 onwards: 2,4