C Requires Chilkat v11.7.0+
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 C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject 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 = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/order_records.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
return;
}
json = CkJsonObject_Create();
jsonOut = CkJsonObject_Create();
// Find every order number. With regex set to true, the query is a regular expression.
CkJsonObject_UpdateString(json,"query","ORD-[0-9]{4}-[0-9]{4}");
CkJsonObject_UpdateBool(json,"regex",TRUE);
// We only want the matched text, so no context is needed.
CkJsonObject_UpdateInt(json,"context",0);
success = CkPdf_ExtractText(pdf,"search",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("Order numbers found: %d\n",CkJsonObject_IntOf(jsonOut,"count"));
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonOut,"matches");
while (i < count_i) {
CkJsonObject_putI(jsonOut,i);
printf("%s\n",CkJsonObject_stringOf(jsonOut,"matches[i].text"));
i = i + 1;
}
// Find the e-mail addresses, but stop after the first three.
CkJsonObject_UpdateString(json,"query","[a-z0-9._]+@[a-z]+[.][a-z]+");
CkJsonObject_UpdateInt(json,"maxResults",3);
success = CkPdf_ExtractText(pdf,"search",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("First three e-mail addresses:\n");
i = 0;
count_i = CkJsonObject_SizeOfArray(jsonOut,"matches");
while (i < count_i) {
CkJsonObject_putI(jsonOut,i);
printf("%s\n",CkJsonObject_stringOf(jsonOut,"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".
CkJsonObject_Delete(json,"maxResults");
CkJsonObject_UpdateBool(json,"regex",FALSE);
CkJsonObject_UpdateString(json,"query","cat");
success = CkPdf_ExtractText(pdf,"search",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("Occurrences of cat (any): %d\n",CkJsonObject_IntOf(jsonOut,"count"));
CkJsonObject_UpdateBool(json,"wholeWord",TRUE);
success = CkPdf_ExtractText(pdf,"search",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
printf("Occurrences of cat (whole word): %d\n",CkJsonObject_IntOf(jsonOut,"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
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}