Delphi ActiveX Requires Chilkat v11.7.0+
Delphi ActiveX
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 Delphi ActiveX Downloads
var
success: Integer;
pdf: TChilkatPdf;
json: TChilkatJsonObject;
jsonOut: TChilkatJsonObject;
i: Integer;
count_i: Integer;
begin
success := 0;
// 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 := TChilkatPdf.Create(Self);
success := pdf.LoadFile('qa_data/pdf/order_records.pdf');
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
json := TChilkatJsonObject.Create(Self);
jsonOut := TChilkatJsonObject.Create(Self);
// Find every order number. With regex set to true, the query is a regular expression.
json.UpdateString('query','ORD-[0-9]{4}-[0-9]{4}');
json.UpdateBool('regex',1);
// We only want the matched text, so no context is needed.
json.UpdateInt('context',0);
success := pdf.ExtractText('search',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Order numbers found: ' + IntToStr(jsonOut.IntOf('count')));
i := 0;
count_i := jsonOut.SizeOfArray('matches');
while i < count_i do
begin
jsonOut.I := i;
Memo1.Lines.Add(jsonOut.StringOf('matches[i].text'));
i := i + 1;
end;
// Find the e-mail addresses, but stop after the first three.
json.UpdateString('query','[a-z0-9._]+@[a-z]+[.][a-z]+');
json.UpdateInt('maxResults',3);
success := pdf.ExtractText('search',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('First three e-mail addresses:');
i := 0;
count_i := jsonOut.SizeOfArray('matches');
while i < count_i do
begin
jsonOut.I := i;
Memo1.Lines.Add(jsonOut.StringOf('matches[i].text'));
i := i + 1;
end;
// wholeWord: true matches only whole words. The word "cat" appears once in the PDF,
// but it is also inside "catalog" and "category".
json.Delete('maxResults');
json.UpdateBool('regex',0);
json.UpdateString('query','cat');
success := pdf.ExtractText('search',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Occurrences of cat (any): ' + IntToStr(jsonOut.IntOf('count')));
json.UpdateBool('wholeWord',1);
success := pdf.ExtractText('search',json.ControlInterface,jsonOut.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(pdf.LastErrorText);
Exit;
end;
Memo1.Lines.Add('Occurrences of cat (whole word): ' + IntToStr(jsonOut.IntOf('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