Unicode C Requires Chilkat v11.7.0+
Unicode C
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 Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
success = FALSE;
// 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 = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/invoice_labels.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// 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.
CkJsonObjectW_UpdateString(json,L"label",L"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.)
CkJsonObjectW_UpdateString(json,L"labelValue",L"right");
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// The value is returned as the text.
wprintf(L"Invoice Number: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// The labelMatches array tells where the label was found and how it appeared in the PDF.
wprintf(L"Found on page %d as \"%s\"\n",CkJsonObjectW_IntOf(jsonOut,L"labelMatches[0].page"),CkJsonObjectW_stringOf(jsonOut,L"labelMatches[0].label"));
// Matching ignores case and the trailing colon, so these all work:
CkJsonObjectW_UpdateString(json,L"label",L"CUSTOMER:");
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Customer: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
CkJsonObjectW_UpdateString(json,L"label",L"due date");
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Due Date: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// The totals at the foot of the invoice are right-aligned labels with the amount beside them.
CkJsonObjectW_UpdateString(json,L"label",L"Total Due");
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Total Due: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// 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".
CkJsonObjectW_UpdateString(json,L"label",L"Tracking Number");
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
if (CkJsonObjectW_SizeOfArray(jsonOut,L"labelMatches") == 0) {
wprintf(L"Tracking Number: (not found) %s\n",CkJsonObjectW_stringOf(jsonOut,L"quality.pages[0].issues[0].code"));
}
// 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
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}