PDFium Component Docs

RectangleCount-metode

Component: TPdf  ·  Unit: PDFium
Teller antall rektangulære områder som er opptatt av et tekstsegment. Funksjonen slår automatisk sammen små tegnbokser til én større hvis tegnene står på samme linje og bruker samme skriftinnstillinger

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

Antallet sammenslåtte avgrensningsrektangler som dekker det angitte tegnsegmentet. Etter dette kallet er hvert rektangel tilgjengelig via den indekserte egenskapen Rectangle[0] til Rectangle[RectangleCount - 1]

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