Objective-C Requires Chilkat v11.7.0+
Objective-C
Page Numbers, Dates and the File Name in a PDF Watermark
See more PDF Watermark Examples
Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.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;
}
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"type" value: @"text"];
[json UpdateString: @"text" value: @"Page {page} of {pages} - printed {date:yyyy-MM-dd HH:mm} - {filename}"];
[json UpdateString: @"id" value: @"pagefoot"];
[json UpdateString: @"position.align" value: @"bottom"];
[json UpdateNumber: @"position.margin" numericStr: @"24"];
[json UpdateNumber: @"font.size" numericStr: @"9"];
[json UpdateString: @"color" value: @"#505050"];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf Watermark: @"add" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%@",@"footer pages: ",[jsonOut StringOf: @"pages"]);
// Roman numerals that start at i on page 2, so that a cover page is not numbered.
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: @"- {page} -"];
[json2 UpdateString: @"id" value: @"roman"];
[json2 UpdateString: @"pages" value: @"2-"];
[json2 UpdateInt: @"pageNumber.start" value: [NSNumber numberWithInt: 1]];
[json2 UpdateString: @"pageNumber.style" value: @"romanLower"];
[json2 UpdateString: @"position.align" value: @"bottom"];
[json2 UpdateNumber: @"font.size" numericStr: @"10"];
success = [pdf2 Watermark: @"add" jsonIn: json2 jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf2.LastErrorText);
return;
}
NSLog(@"%@%@",@"roman pages: ",[jsonOut StringOf: @"pages"]);
success = [pdf WriteFile: @"qa_output/four_pages_numbered.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// Sample output:
// footer pages: 1-4
// roman pages: 2-4