Lazarus Pascal Requires Chilkat v11.7.0+
Lazarus Pascal
Clipped, Off-Page and White-on-White Text in PDF Extraction
See more PDF Text Extraction Examples
Demonstrates the "clippedText", "offPageText" and "obscuredText" filters. Text that a reader never sees - hidden by a clipping path, outside the crop box, or drawn in the same color as the box beneath it - is excluded by default, and the quality report notes what was skipped. Each kind can be included.Chilkat Lazarus Pascal Downloads
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 PDF used in this example is available at hidden_text.pdf
pdf := TPdf.Create;
success := pdf.LoadFile('qa_data/pdf/hidden_text.pdf');
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
json := TJsonObject.Create;
jsonOut := TJsonObject.Create;
// The page has a visible paragraph plus three hidden lines: one drawn outside its clipping
// rectangle, one below the crop box, and one in white on a white box.
success := pdf.ExtractText('text',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('---- default ----');
WriteLn(jsonOut.StringOf('text'));
// The quality report says what was left out.
i := 0;
count_i := jsonOut.SizeOfArray('quality.pages[0].issues');
while i < count_i do
begin
jsonOut.I := i;
WriteLn('issue: ' + jsonOut.StringOf('quality.pages[0].issues[i].code'));
i := i + 1;
end;
// Include everything.
json.UpdateString('clippedText','include');
json.UpdateString('offPageText','include');
json.UpdateString('obscuredText','include');
success := pdf.ExtractText('text',json,jsonOut);
if (success = False) then
begin
WriteLn(pdf.LastErrorText);
Exit;
end;
WriteLn('---- clippedText, offPageText, obscuredText: include ----');
WriteLn(jsonOut.StringOf('text'));
// Sample output:
// ---- default ----
// Visible Text
//
// This paragraph is visible. The page also carries three kinds of text a reader never sees.
//
// End of the visible text.
// issue: obscuredTextSkipped
// issue: clippedTextSkipped
// ---- clippedText, offPageText, obscuredText: include ----
// Visible Text
//
// This paragraph is visible. The page also carries three kinds of text a reader never sees.
//
// CLIPPED: this line is drawn outside its clipping rectangle.
//
// OBSCURED: this line is white on a white box.
//
// End of the visible text.
//
// OFFPAGE: this line sits below the crop box.
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.