وثائق PDFiumVCL

TextInRectangle method

يحافظ إدخال API هذا على المعرّفات والتواقيع وكتل الكود ومصطلحات PDF بصيغتها الأصلية.
Component: TPdf  ·  Unit: PDFium
Extract text within a rectangular boundary on the page.

Syntax

function TextInRectangle(Left, Top, Right, Bottom: Double): WString;

 

function TextInRectangle(Rectangle: TPdfRectangle): WString;

RectangleTPdfRectangle. A record specifying the bounding rectangle in PDF user-space coordinates (origin at lower-left, Y increases upward).

Return Value

A WString (Unicode string) containing all characters whose bounding boxes intersect the specified rectangle. Returns an empty string if no text is found within the region.

Description

TextInRectangle extracts the Unicode text content that falls within a given rectangular region on the current page. This is useful for reading text from a specific area such as a header, footer, table cell, or field label without processing the entire page.

Two overloads are provided. The four-parameter form accepts individual Left, Top, Right, and Bottom values. The single-parameter form accepts a TPdfRectangle record, which is convenient when the region is already computed as a rectangle structure.

Coordinates use PDF user space: the origin is at the lower-left corner of the page and Y increases upward. Note that Bottom is therefore a smaller value than Top for a normally oriented rectangle. Characters are included in the result if their bounding boxes overlap the specified region.

Like Text, this method reads from the page's logical text layer and operates on a loaded PDF page.

Example

// Extract text from the top header area of an A4 page
// A4: 595 x 842 pt; header = top 60 pt band
Pdf1.LoadFromFile('C:\Docs\report.pdf');
Pdf1.PageIndex := 0;
var HeaderText: WString;
begin
  HeaderText := Pdf1.TextInRectangle(0, 782, 595, 842);
  ShowMessage(HeaderText);
end;

// Using TPdfRectangle overload
var
  Rect: TPdfRectangle;
  CellText: WString;
begin
  Rect.Left := 72; Rect.Bottom := 600;
  Rect.Right := 250; Rect.Top := 640;
  CellText := Pdf1.TextInRectangle(Rect);
end;

See Also

Text, CharacterCount, FindFirst