Text Selection ​
Foxit PDF SDK for Web lets users select text in the Viewer and perform actions such as copy, create text markup annotations, or integrate custom business tools. With the full UI (UIExtension), a floating toolbar after text selection is built in; when you need a custom toolbar or to read selection data, use the public APIs on TextSelectionTool.
Related APIs include:
TextSelectionTool.getSelectionInfo(): Returns the currently selected text, the page object, and selection rectangles.TextSelectionTool.copy(): Copies the currently selected text.
Get Selection Info ​
getSelectionInfo() returns information about the current text selection, including the selected text, the page object, and selection rectangles per line.
javascript
const selectionInfo = await textSelectionTool.getSelectionInfo();
if (selectionInfo) {
console.log(selectionInfo.text);
console.log(selectionInfo.page.getIndex());
console.log(selectionInfo.rectArray);
}Rectangles in rectArray can be used to create text markup annotations or for business-side positioning. For cross-page selections, selection info may include data from multiple pages; split handling by page when processing.
Copy Selected Text ​
javascript
await textSelectionTool.copy();Copying to the clipboard usually must run inside a user-triggered event and is subject to browser security policies.
Work with Text Markup Annotations ​
The full UI text-selection floating toolbar typically provides highlight, underline, strikethrough, and similar actions. In a custom flow, call getSelectionInfo() first to obtain selected text and rectangles, then create the corresponding text markup annotation using the public PDFPage.addAnnot() API described in Annotations.
After creating or modifying annotations, export the PDF through the document save workflow if you need to persist changes to the file.
Notes ​
- Text selection depends on parseable text in the PDF; scanned documents usually require OCR before text can be selected.
- When using selection rectangles for Viewer interaction or annotation creation, account for the difference between PDF coordinates and device pixels.
- To read text or character positions in bulk, use Text extraction.
- To find and locate content by keyword, use Text search.