PDFium Delphi Component 文件

TextInRectangle 方法

元件: TPdf  ·  單元: PDFium
擷取頁面中矩形邊界內的文字

Syntax

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

 

function TextInRectangle(Rectangle: TPdfRectangle): WString;

RectangleTPdfRectangle。以 PDF 使用者空間座標指定週界矩形的記錄(原點在左下,Y 軸向上遞增)

Return Value

包含週界框與指定矩形相交之所有字元的 WString(Unicode 字串)。若區域內找不到文字,則傳回空字串

Description

TextInRectangle 會擷取目前頁面上落在指定矩形區域內的 Unicode 文字內容。適合只讀取特定區域(例如頁首、頁尾、表格儲存格或欄位標籤)的文字,而無須處理整頁

提供兩種多載。四參數形式接受個別的 Left、Top、Right 與 Bottom 值。單參數形式接受 TPdfRectangle 記錄,當區域已計算為矩形結構時更為方便

座標採用 PDF 使用者空間:原點位於頁面左下角,Y 軸向上遞增。請注意,因此在正常方向的矩形中,Bottom 的值會小於 Top。字元的週界框若與指定區域重疊,就會包含在結果中

與 Text 相同,此方法讀取頁面的邏輯文字層,且作用於已載入的 PDF 頁面

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