PDFium Component Docs

CharacterIndexAtPos-metodi

Component: TPdf  ·  Unit: PDFium
Hakee merkin indeksin tietystä kohdasta tai sen läheltä sivulla. Jos pisteessä tai sen läheisyydessä ei ole merkkiä, palautusarvo on -1. Virhetilanteessa palautetaan -3

Syntax

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

Return Value

Lähimmän merkin nollapohjainen indeksi, -1 jos mikään merkki ei osu määritettyyn toleranssialueeseen, tai -3 jos sisäinen virhe tapahtuu (esimerkiksi jos sivua ei ole ladattu)

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