Skip to main content

GetSelection

Gets page selection.

Syntax​

expression.GetSelection();

expression - A variable that represents an ApiPage class.

Parameters​

This method doesn't have any parameters.

Returns​

PageSelection | null | undefined

Example​

Get the current selection on a page in a PDF.

// Read back the selection range set on a single page as start and end points.

// Search for text on the page, select it, then log the selection's start and end coordinates.

let doc = Api.GetDocument();
doc.AddPage(0);
let page = doc.GetPage(0);
let fill = Api.CreateSolidFill(Api.RGB(50, 150, 250));
let stroke = Api.CreateStroke(0, Api.CreateNoFill());
let shape = Api.CreateShape("rect", 150 * 36000, 80 * 36000, fill, stroke);
shape.SetPosition(10 * 12700, 10 * 12700);
let paragraph = shape.GetContent().GetElement(0);
paragraph.SetJc("left");
paragraph.AddText("Lorem selected text");
page.AddObject(shape);

let results = page.Search({text: "Lorem selected text", matchCase: true, wholeWords: false});
let quads = results[0];
let firstQuad = quads[0];
let lastQuad = quads[quads.length - 1];
page.SetSelection({ start: { x: firstQuad[0], y: firstQuad[1] }, end: { x: lastQuad[2], y: lastQuad[3] } });
let selection = page.GetSelection();
console.log('Selection start - x: ' + selection.start.x + ', y: ' + selection.start.y);
console.log('Selection end - x: ' + selection.end.x + ', y: ' + selection.end.y);