Objective-C Requires Chilkat v11.7.0+
Objective-C
Check a PDF Text Replacement Before Applying It
See more PDF Text Replacement Examples
Demonstrates the "check" operation, which finds every placeholder and label and applies the font rule but changes nothing, and how to read its report: "ok" says whether "apply" would succeed, "replacements" lists what would be replaced, and "problems" lists what prevents it, each with a reason. Here the first check fails because a placeholder on the page was given no value; the value is added and the check passes, and only then is "apply" called.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_template.pdf
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/invoice_template.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
// Values for most of the template's placeholders -- but not {{po}}, {{qty2}}, {{desc2}}, {{price2}} or {{amount2}}.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"placeholders.customer" value: @"Acme Corp"];
[json UpdateString: @"placeholders.date" value: @"2026-10-18"];
[json UpdateString: @"placeholders.qty1" value: @"2"];
[json UpdateString: @"placeholders.desc1" value: @"Cordless drill, 18 V"];
[json UpdateString: @"placeholders.price1" value: @"$460.00"];
[json UpdateString: @"placeholders.amount1" value: @"$920.00"];
[json UpdateString: @"placeholders.subtotal" value: @"$920.00"];
[json UpdateString: @"placeholders.taxRate" value: @"7.35%"];
[json UpdateString: @"placeholders.tax" value: @"$67.62"];
[json UpdateString: @"placeholders.total" value: @"$987.62"];
// "check" reports what "apply" would do, without changing the PDF.
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf TextReplace: @"check" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"ok: ",[jsonOut BoolOf: @"ok"]);
NSLog(@"%@%d%@",@"would replace: ",[[jsonOut SizeOfArray: @"replacements"] intValue],@" placeholders");
// Each problem names the placeholder (field) or label, and a reason:
// notFound - the placeholder or label is not on the selected pages
// unusedPlaceholder - a placeholder on the page was given no value
// missingGlyph, notInEncoding, nonStandardUnembeddedFont, unsupportedFont - the font rule
// placeholderSpansFonts - the text is drawn with more than one font or size
int i = 0;
int count_i = [[jsonOut SizeOfArray: @"problems"] intValue];
while (i < count_i) {
jsonOut.I = [NSNumber numberWithInt: i];
NSLog(@"%@%@%@%@%@%@%d%@",@"problem: ",[jsonOut StringOf: @"problems[i].field"],[jsonOut StringOf: @"problems[i].label"],@" - ",[jsonOut StringOf: @"problems[i].reason"]
,@" (page ",[[jsonOut IntOf: @"problems[i].page"] intValue],@")");
i = i + 1;
}
// Give the missing placeholders their values and check again.
[json UpdateString: @"placeholders.po" value: @"PO-88213"];
[json UpdateString: @"placeholders.qty2" value: @"0"];
[json UpdateString: @"placeholders.desc2" value: @"(no second item)"];
[json UpdateString: @"placeholders.price2" value: @""];
[json UpdateString: @"placeholders.amount2" value: @""];
success = [pdf TextReplace: @"check" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"ok after adding the values: ",[jsonOut BoolOf: @"ok"]);
// Now apply.
if ([jsonOut BoolOf: @"ok"] == YES) {
success = [pdf TextReplace: @"apply" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"applied: ",[jsonOut BoolOf: @"applied"]);
success = [pdf WriteFile: @"qa_output/invoice_checked.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
}
// Sample output:
// ok: false
// would replace: 10 placeholders
// problem: po - unusedPlaceholder (page 1)
// problem: qty2 - unusedPlaceholder (page 1)
// problem: desc2 - unusedPlaceholder (page 1)
// problem: price2 - unusedPlaceholder (page 1)
// problem: amount2 - unusedPlaceholder (page 1)
// ok after adding the values: true
// applied: true