Docs PDFiumVCL

RectangleCount method

Esta entrada API preserva identificadores, assinaturas, blocos de código e termos PDF em sua forma original.
Component: TPdf  ·  Unit: PDFium
Count number of rectangular areas occupied by a segment of texts. Function automatically merges small character boxes into bigger one if those characters are on the same line and use same font settings.

Syntax

function RectangleCount(StartIndex: Integer = 0; Count: Integer = MaxInt): Integer;

StartIndexInteger. The 0-based index of the first character in the segment to measure. Defaults to 0.
CountInteger. The number of characters to include in the measurement, starting from StartIndex. Defaults to MaxInt, which covers all remaining characters on the page.

Return Value

The number of merged bounding rectangles that cover the specified character segment. After this call, each rectangle is accessible via the Rectangle[0] through Rectangle[RectangleCount - 1] indexed property.

Description

RectangleCount calculates the set of bounding rectangles that together cover a contiguous run of characters on the current page. Characters on the same line that share the same font settings are automatically merged into a single rectangle, reducing the result to the minimum number of non-overlapping boxes needed to highlight the text.

The StartIndex and Count parameters define the character segment to measure. A typical use is to pass the match index returned by FindFirst or FindNext as StartIndex, and the length of the search string as Count, in order to obtain highlight rectangles for a search result.

The returned rectangles are stored in an internal buffer and remain valid until the next call to RectangleCount. Retrieve each rectangle by reading the Rectangle[Index] property for Index in the range 0 to RectangleCount - 1. All coordinates are in PDF user-space units (points, origin at lower-left).

For a plain count of all characters on the page use CharacterCount; to find where a specific character sits on the page use CharacterIndexAtPos.

Example

var
  MatchIdx, RCount, I: Integer;
  Rect: TPdfRectangle;
begin
  Pdf1.PageIndex := 0;
  MatchIdx := Pdf1.FindFirst('PDFium');
  if MatchIdx >= 0 then
  begin
    RCount := Pdf1.RectangleCount(MatchIdx, Length('PDFium'));
    for I := 0 to RCount - 1 do
    begin
      Rect := Pdf1.Rectangle[I];
      // Draw a highlight box using Rect.Left/Top/Right/Bottom
      Memo1.Lines.Add(Format('Rect %d: (%.1f, %.1f, %.1f, %.1f)',
        [I, Rect.Left, Rect.Bottom, Rect.Right, Rect.Top]));
    end;
  end;
end;

See Also

Rectangle, FindFirst, CharacterCount, CharacterIndexAtPos, Text