GetSelectedText
Gets selected text on page
Syntax
expression.GetSelectedText();
expression - A variable that represents an ApiPage class.
Parameters
This method doesn't have any parameters.
Returns
string
Example
Extract the text that has been highlighted on a page in a PDF.
// Useful for capturing the text selected in a PDF.
// Retrieve what text is currently marked on a page in a PDF.
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]}});
console.log(`The selected text is: ${page.GetSelectedText()}`);