PDFium Component Docs

CharacterIndexAtPos method

Component: TPdf  ·  Unit: PDFium
Lấy chỉ số của một ký tự tại hoặc gần một vị trí nhất định trên trang. Nếu không có ký tự nào tại hoặc gần điểm đó, giá trị trả về sẽ là -1. Nếu xảy ra lỗi, sẽ trả về -3

Syntax

function CharacterIndexAtPos(X, Y, ToleranceX, ToleranceY: Double): Integer;

Return Value

The 0-based index of the nearest character, -1 if no character falls within the specified tolerance region, or -3 nếu xảy ra lỗi nội bộ (ví dụ, nếu không có trang nào được nạp)

Description

CharacterIndexAtPos maps a point in PDF user-space coordinates to the index of the nearest character on the current page. It is the inverse operation of reading Character[Index] or RectangleCount: given a position (such as a mouse click translated to page coordinates), it identifies which character the user pointed at.

The X and Y parameters specify the query point in PDF user-space units. The origin is at the lower-left corner of the page, with Y increasing upward. The ToleranceX and ToleranceY parameters extend a rectangular search region around the query point; any character whose bounding box intersects that region is a candidate.

If no character is found within the tolerance region the function returns -1. A return value of -3 indicates an error, typically because no page is currently open. On success the returned index can be passed directly to RectangleCount, Character, or used as the StartIndex argument of FindFirst.

When converting screen (pixel) coordinates to PDF user-space coordinates, use the page width and height together with the current zoom level to scale appropriately.

Example

var
  CharIdx: Integer;
  PageX, PageY: Double;
begin
  Pdf1.PageIndex := 0;
  // PageX/PageY are already converted to PDF user-space units
  PageX := 150.0;
  PageY := 400.0;
  CharIdx := Pdf1.CharacterIndexAtPos(PageX, PageY, 5.0, 5.0);
  if CharIdx >= 0 then
    ShowMessage('Character: ' + Pdf1.Character[CharIdx])
  else if CharIdx = -1 then
    ShowMessage('No character at that position')
  else
    ShowMessage('Error retrieving character');
end;

See Also

CharacterCount, Character, FindFirst, RectangleCount