PHP Requires Chilkat v11.7.0+
Chilkat2 PHP
Get a PDF Field Value by its Label (Value to the Right)
See more PDF Text Extraction Examples
Demonstrates the "label" option, which captures the text that belongs to a caption such as "Invoice Number:" without needing coordinates. With labelValue "right", the value is the rest of the label's line. Label matching ignores case, extra spaces and a trailing colon.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 invoice 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();
// The invoice has lines such as "Invoice Number: INV-2026-0412".
// The label option restricts the "text" operation to the text belonging to the label.
$json->UpdateString('label', 'Invoice Number');
// "right" means the value is the rest of the label's line.
// (The default, "auto", uses the rest of the line if there is text there, otherwise the block below.)
$json->UpdateString('labelValue', 'right');
$pdf->ExtractText('text', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
// The value is returned as the text.
echo 'Invoice Number: ', $jsonOut->StringOf('text'), "\n";
// The labelMatches array tells where the label was found and how it appeared in the PDF.
echo 'Found on page ', $jsonOut->IntOf('labelMatches[0].page'), ' as "', $jsonOut->StringOf('labelMatches[0].label'), "\"\n";
// Matching ignores case and the trailing colon, so these all work:
$json->UpdateString('label', 'CUSTOMER:');
try {
$pdf->ExtractText('text', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Customer: ', $jsonOut->StringOf('text'), "\n";
$json->UpdateString('label', 'due date');
try {
$pdf->ExtractText('text', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Due Date: ', $jsonOut->StringOf('text'), "\n";
// The totals at the foot of the invoice are right-aligned labels with the amount beside them.
$json->UpdateString('label', 'Total Due');
try {
$pdf->ExtractText('text', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo 'Total Due: ', $jsonOut->StringOf('text'), "\n";
// A label that is not found is not an error. The text is empty, labelMatches is empty,
// and the quality report has an issue with the code "labelNotFound".
$json->UpdateString('label', 'Tracking Number');
try {
$pdf->ExtractText('text', $json, $jsonOut);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
if ($jsonOut->SizeOfArray('labelMatches') == 0) {
echo 'Tracking Number: (not found) ', $jsonOut->StringOf('quality.pages[0].issues[0].code'), "\n";
}
// Sample output:
// Invoice Number: INV-2026-0412
// Found on page 1 as "Invoice Number:"
// Customer: Northwind Traders
// Due Date: April 13, 2026
// Total Due: $1,234.53
// Tracking Number: (not found) labelNotFound