Class: TextSelection

PSPDFKit.TextSelection

Information about the currently selected text in the PDF. You can listen for changes using the PSPDFKit.Instance~TextSelectionChangeEvent.

Example

Read the currently selected text of an Instance

const textSelection = instance.getTextSelection();
textSelection.getText().then(text => console.log(text));

Register a TextSelectionChangeEvent

instance.addEventListener("textSelection.change", (textSelection) => {
  if (textSelection) {
    console.log("text is selected");
  } else {
    console.log("no text is selected");
  }
});

Extends

  • Immutable.Record

Members

Methods




Members

endNode: Text

The HTML Text node at the end of this text selection.

Type:

endOffset: number

The HTML Text node offset of at the end of this text selection.

Type:
  • number

endPageIndex: number

The page index where the text selection ends.

Type:
  • number

endTextLineId: number

The PSPDFKit.TextLine#id of the last text line of this text selection.

Type:
  • number

startNode: Text

The HTML Text node at the start of this text selection.

Type:

startOffset: number

The HTML Text node offset of at the start of this text selection.

Type:
  • number

startPageIndex: number

The page index where the text selection starts.

Type:
  • number

startTextLineId: number

The PSPDFKit.TextLine#id of the first text line of this text selection.

Type:
  • number

Methods

(static) getBoundingClientRect() → {Promise.<(PSPDFKit.Geometry.Rect|null)>}

Returns the bounding box in client coordinates of the current text selection, or null if the selection has been programmatically collapsed.

Returns:

A promise that resolves to the client rect.

Type
Promise.<(PSPDFKit.Geometry.Rect|null)>
Example
const textSelection = instance.getTextSelection();
textSelection.getBoundingClientRect().then(rect => console.log(rect));

(static) getSelectedRectsPerPage() → {Promise.<PSPDFKit.Immutable.List.<{pageIndex: number, rects: PSPDFKit.Immutable.List.<PSPDFKit.Geometry.Rect>}>>}

Returns the individually selected text rectangles in the PDF page space for each page. This can be used to create e.g. PSPDFKit.Annotations.MarkupAnnotations at the current selection.

Returns:

A promise that resolves to a list of rects per page.

Type
Promise.<PSPDFKit.Immutable.List.<{pageIndex: number, rects: PSPDFKit.Immutable.List.<PSPDFKit.Geometry.Rect>}>>
Example
const textSelection = instance.getTextSelection();
textSelection.getSelectedRectsPerPage().then(rectsPerPage => {
  rectsPerPage.map(({ pageIndex, rects }) => {
    // We need to create one annotation per page.
    const annotation = new PSPDFKit.Annotations.HighlightAnnotation({
      pageIndex,
      boundingBox: PSPDFKit.Geometry.Rect.union(rects),
      rects,
    });
    instance.create(annotation);
  });
});

(static) getSelectedTextLines() → {Promise.<PSPDFKit.Immutable.List.<PSPDFKit.TextLine>>}

Returns an immutable list of all PSPDFKit.TextLines of this text selection.

Returns:

A promise that resolves to a list of all text lines.

Type
Promise.<PSPDFKit.Immutable.List.<PSPDFKit.TextLine>>
Example
const textSelection = instance.getTextSelection();
textSelection.getSelectedTextLines().then(lines => console.log(lines));

(static) getText() → {Promise.<string>}

Returns the text of this text selection. Text blocks will be joined by a new line character (\n).

Returns:

A promise that resolves to the text.

Type
Promise.<string>
Example
const textSelection = instance.getTextSelection();
textSelection.getText().then(text => console.log(text));