Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.7.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkPdf;
json: HCkJsonObject;
jsonOut: HCkJsonObject;
i: Integer;
count_i: Integer;

begin
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 := CkPdf_Create();
success := CkPdf_LoadFile(pdf,'qa_data/pdf/three_receipts.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

json := CkJsonObject_Create();
jsonOut := CkJsonObject_Create();

//  Each of the three pages is a receipt with a "Total:" line.
CkJsonObject_UpdateString(json,'label','Total');
CkJsonObject_UpdateString(json,'labelValue','right');

//  By default only the first occurrence (in reading order) is used.
success := CkPdf_ExtractText(pdf,'text',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('First total: ' + CkJsonObject__stringOf(jsonOut,'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.
CkJsonObject_UpdateString(json,'labelOccurrence','all');
success := CkPdf_ExtractText(pdf,'text',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

i := 0;
count_i := CkJsonObject_SizeOfArray(jsonOut,'labelMatches');
while i < count_i do
  begin
    CkJsonObject_putI(jsonOut,i);
    Memo1.Lines.Add('Page ' + IntToStr(CkJsonObject_IntOf(jsonOut,'labelMatches[i].page')) + ' total: ' + CkJsonObject__stringOf(jsonOut,'labelMatches[i].text'));
    i := i + 1;
  end;

//  A number selects one occurrence: 2 is the second.
CkJsonObject_UpdateInt(json,'labelOccurrence',2);
success := CkPdf_ExtractText(pdf,'text',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('Second total: ' + CkJsonObject__stringOf(jsonOut,'text'));

//  The occurrences are counted over the selected pages, so combining "pages" with the label
//  is another way to pick one receipt:
CkJsonObject_UpdateString(json,'labelOccurrence','first');
CkJsonObject_UpdateString(json,'pages','3');
CkJsonObject_UpdateString(json,'label','Receipt No.');
success := CkPdf_ExtractText(pdf,'text',json,jsonOut);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;
Memo1.Lines.Add('Receipt number on page 3: ' + CkJsonObject__stringOf(jsonOut,'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

CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);