Unicode C Requires Chilkat v11.7.0+
Unicode C
Search a PDF with a Regular Expression
See more PDF Text Extraction Examples
Demonstrates searching a PDF with a regular expression (regex: true), limiting the number of results (maxResults), and matching whole words only (wholeWord).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 is available at order_records.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/order_records.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// Find every order number. With regex set to true, the query is a regular expression.
CkJsonObjectW_UpdateString(json,L"query",L"ORD-[0-9]{4}-[0-9]{4}");
CkJsonObjectW_UpdateBool(json,L"regex",TRUE);
// We only want the matched text, so no context is needed.
CkJsonObjectW_UpdateInt(json,L"context",0);
success = CkPdfW_ExtractText(pdf,L"search",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Order numbers found: %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"matches");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"matches[i].text"));
i = i + 1;
}
// Find the e-mail addresses, but stop after the first three.
CkJsonObjectW_UpdateString(json,L"query",L"[a-z0-9._]+@[a-z]+[.][a-z]+");
CkJsonObjectW_UpdateInt(json,L"maxResults",3);
success = CkPdfW_ExtractText(pdf,L"search",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 three e-mail addresses:\n");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jsonOut,L"matches");
while (i < count_i) {
CkJsonObjectW_putI(jsonOut,i);
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"matches[i].text"));
i = i + 1;
}
// wholeWord: true matches only whole words. The word "cat" appears once in the PDF,
// but it is also inside "catalog" and "category".
CkJsonObjectW_Delete(json,L"maxResults");
CkJsonObjectW_UpdateBool(json,L"regex",FALSE);
CkJsonObjectW_UpdateString(json,L"query",L"cat");
success = CkPdfW_ExtractText(pdf,L"search",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Occurrences of cat (any): %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));
CkJsonObjectW_UpdateBool(json,L"wholeWord",TRUE);
success = CkPdfW_ExtractText(pdf,L"search",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"Occurrences of cat (whole word): %d\n",CkJsonObjectW_IntOf(jsonOut,L"count"));
// Sample output:
// Order numbers found: 9
// ORD-2026-0301
// ORD-2026-0302
// ORD-2026-0303
// ORD-2026-0304
// ORD-2026-0305
// ORD-2026-0306
// ORD-2026-0307
// ORD-2026-0308
// ORD-2026-0307
// First three e-mail addresses:
// alice.moore@example.com
// bkumar@example.org
// carla@example.net
// Occurrences of cat (any): 3
// Occurrences of cat (whole word): 1
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}