PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
A PDF Label That Is Not on the Page: notFound or Ignore
See more PDF Text Replacement Examples
Demonstrates the "unmatchedValues" option. A label or placeholder given a value but not found on the selected pages is a problem with the reason notFound, which stops "apply", because a missing label usually means the wrong document or a typo in the label. With unmatchedValues "ignore" such a value is skipped and the rest is applied -- the right choice when one set of values serves several document layouts.Chilkat PHP Downloads
<?php
use Chilkat2\JsonObject;
use Chilkat2\Pdf;
// 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_labels.pdf
$pdf = new Pdf();
try {
$pdf->LoadFile('qa_data/pdf/invoice_labels.pdf');
$json = new JsonObject();
$jsonOut = new JsonObject();
// "Tracking Number:" is not on this invoice.
$json->UpdateString('labels[0].label', 'Invoice Number:');
$json->UpdateString('labels[0].value', 'INV-2026-0500');
$json->UpdateString('labels[1].label', 'Tracking Number:');
$json->UpdateString('labels[1].value', '1Z999AA10123456784');
$pdf->TextReplace('check', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'default: ok = ', ($jsonOut->BoolOf('ok') ? 'true' : 'false'), ', problem: "', $jsonOut->StringOf('problems[0].label'), '" ', $jsonOut->StringOf('problems[0].reason'), "\n";
// With "ignore", the label that is not there is skipped.
$json->UpdateString('unmatchedValues', 'ignore');
try {
$pdf->TextReplace('apply', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'ignore: applied = ', ($jsonOut->BoolOf('applied') ? 'true' : 'false'), ', replacements: ', $jsonOut->SizeOfArray('replacements'), "\n";
try {
$pdf->WriteFile('qa_output/invoice_renumbered.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Sample output:
// default: ok = false, problem: "Tracking Number:" notFound
// ignore: applied = true, replacements: 1