Unicode C Requires Chilkat v11.7.0+
Unicode C
Get Every Occurrence of a Label in a PDF
See more PDF Text Extraction Examples
Demonstrates the "labelOccurrence" option. When the same label appears more than once (for example "Total:" on each of several receipts), "first" (the default) uses the first occurrence, "all" returns every occurrence with its page, and a number selects one occurrence.Chilkat Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
int i;
int count_i;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example (three receipts, one per page) is available at three_receipts.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/three_receipts.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// Each of the three pages is a receipt with a "Total:" line.
CkJsonObjectW_UpdateString(json,L"label",L"Total");
CkJsonObjectW_UpdateString(json,L"labelValue",L"right");
// By default only the first occurrence (in reading order) is used.
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"First total: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// labelOccurrence "all" returns every occurrence. The "text" member holds the values joined
// by line breaks, and labelMatches has one entry per occurrence with its page.
CkJsonObjectW_UpdateString(json,L"labelOccurrence",L"all");
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;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"labelMatches");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"Page %d total: %s\n",CkJsonObjectW_IntOf(jsonOut,L"labelMatches[i].page"),CkJsonObjectW_stringOf(jsonOut,L"labelMatches[i].text"));
i = i + 1;
}
// A number selects one occurrence: 2 is the second.
CkJsonObjectW_UpdateInt(json,L"labelOccurrence",2);
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"Second total: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// The occurrences are counted over the selected pages, so combining "pages" with the label
// is another way to pick one receipt:
CkJsonObjectW_UpdateString(json,L"labelOccurrence",L"first");
CkJsonObjectW_UpdateString(json,L"pages",L"3");
CkJsonObjectW_UpdateString(json,L"label",L"Receipt No.");
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"Receipt number on page 3: %s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// Sample output:
// First total: $19.86
// Page 1 total: $19.86
// Page 2 total: $17.34
// Page 3 total: $23.62
// Second total: $17.34
// Receipt number on page 3: 1003
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}