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

// 提取 A4 页面顶部页眉区域的文本
// A4:595 x 842 pt;页眉 = 顶部 60 pt 带
Pdf1.LoadFromFile('C:\Docs\report.pdf');
Pdf1.PageIndex := 0;
var HeaderText: WString;
begin
  HeaderText := Pdf1.TextInRectangle(0, 782, 595, 842);
  ShowMessage(HeaderText);
end;

// 使用 TPdfRectangle 重载
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