PDFiumVCL Docs

CharacterIndexAtPos method

Bu API girdisi tanımlayıcıları, imzaları, kod bloklarını ve PDF terimlerini özgün biçiminde korur.
Component: TPdf  ·  Unit: PDFium
Get the index of a character at or nearby a certain position on the page. If there is no character at or nearby the point, return value will be -1. If an error occurs, -3 will be returned.

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 if an internal error occurs (for example, if no page is loaded).

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