PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
What PDF Text Replacement Does Not Change: Metadata, Links, Form Fields
See more PDF Text Replacement Examples
TextReplace changes the page content only. A placeholder that also appears in the document's metadata (the Title, Author, Subject, Keywords, Creator or Producer), in a link's target URL, or in a form field's value is left as it is and reported in "notReplaced" with "where" saying which. The invoice template's title is "Invoice for {{customer}}" and its "View this invoice online" link points at a URL that holds {{customer}}.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_template.pdf
$pdf = new Pdf();
try {
$pdf->LoadFile('qa_data/pdf/invoice_template.pdf');
$json = new JsonObject();
$json->UpdateString('placeholders.customer', 'Acme Corp');
// (The other placeholders are left as they are for this example.)
$json->UpdateString('unusedPlaceholders', 'keep');
$jsonOut = new JsonObject();
$pdf->TextReplace('apply', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'In the page content, customer was replaced ', $jsonOut->IntOf('replacements[0].count'), " times.\n";
// Where else the placeholder occurs, untouched:
$i = 0;
$count_i = $jsonOut->SizeOfArray('notReplaced');
while ($i < $count_i) {
$jsonOut->I = $i;
echo 'not replaced: {{', $jsonOut->StringOf('notReplaced[i].field'), '}} in ', $jsonOut->StringOf('notReplaced[i].where'), "\n";
$i = $i + 1;
}
try {
$pdf->WriteFile('qa_output/invoice_customer.pdf');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// Sample output:
// In the page content, customer was replaced 2 times.
// not replaced: {{customer}} in metadata
// not replaced: {{customer}} in linkTarget