Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v11.7.0+

Search for Text in a PDF

See more PDF Text Extraction Examples

Demonstrates the "search" operation, which finds every occurrence of a string in a PDF and returns each match with its page number and the text around it. Matching ignores case unless caseSensitive is true.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Pdf,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  pdf: TPdf;
  json: TJsonObject;
  jsonOut: TJsonObject;
  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 three-page PDF used in this example is available at search_context.pdf

  pdf := TPdf.Create;
  success := pdf.LoadFile('qa_data/pdf/search_context.pdf');
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  json := TJsonObject.Create;
  jsonOut := TJsonObject.Create;

  //  The string to find.
  json.UpdateString('query','chilkat');

  //  The number of characters returned before and after each match.  The default is 40.
  json.UpdateInt('context',30);

  success := pdf.ExtractText('search',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  "count" is the number of matches.  Each match has the page, the matched text as it appears
  //  in the PDF, and the text before and after it.
  WriteLn('Matches: ' + jsonOut.IntOf('count'));

  i := 0;
  count_i := jsonOut.SizeOfArray('matches');
  while i < count_i do
    begin
      jsonOut.I := i;
      WriteLn('page ' + jsonOut.IntOf('matches[i].page') + ': ...' + jsonOut.StringOf('matches[i].before') + '[' + jsonOut.StringOf('matches[i].text') + ']' + jsonOut.StringOf('matches[i].after')
           + '...');
      i := i + 1;
    end;

  //  The search ignores case by default.  Set caseSensitive to find only the exact spelling.
  json.UpdateBool('caseSensitive',True);
  json.UpdateString('query','Chilkat');
  success := pdf.ExtractText('search',json,jsonOut);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;
  WriteLn('Case-sensitive matches for Chilkat: ' + jsonOut.IntOf('count'));

  //  Sample output:

  //  Matches: 5
  //  page 1: ...[Chilkat] River Field Notes The Chilkat...
  //  page 1: ...Chilkat River Field Notes The [Chilkat] River runs for about fifty mi...
  //  page 2: ...DAY ONE: UPPER [CHILKAT] FLATS Clear and cold, minus e...
  //  page 2: ...he river is sometimes spelled [chilkat] in older records, and the val...
  //  page 3: ...y because the fish run on the [Chilkat] was thinning. Two tagged bird...
  //  Case-sensitive matches for Chilkat: 3


  pdf.Free;
  json.Free;
  jsonOut.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.