The `highlightPlugin()` function takes a `HighlightPluginProps` parameter that consists of the following properties:
(? denotes an optional property)
Property
Type
Description
From
`renderHighlightTarget?`
`RenderHighlightTargetProps => ReactElement`
Render the element displayed after you select texts. It can be a form that allows user to add a note about selected text
2.3.0
`renderHighlightContent?`
`RenderHighlightContentProps => ReactElement`
Render the highlighted texts before you are going to do something with the selected text
2.3.0
`renderHighlights?`
`RenderHighlightsProps => ReactElement`
Render the texts that are highlighted
2.3.0
`trigger?`
`Trigger`
Indicate when the highlighting is triggered
2.10.0
There are two possible values for the `trigger` option:
import{Trigger}from'@react-pdf-viewer/highlight';
const highlightPluginInstance =highlightPlugin({
trigger:Trigger.None,
});
Value
Description
From
`Trigger.TextSelection`
Show the target after users select text. It is the default value
2.10.0
`Trigger.None`
Doesn't trigger the highlight. It is often used to render the highlight areas
2.10.0
In the next sections, we will go through each property to demonstrate a completed example of highlighting texts.
The plugin instance `highlightPluginInstance` provides the following property:
Property
Type
Description
From
`switchTrigger`
`Function`
Switch the trigger mode
3.8.0
3. Register the plugin
Register the `highlight` plugin instance:
<Viewerplugins={[highlightPluginInstance]}/>
HighlightArea data structure
Imagine that when user selects multiple texts in the PDF document. Each selected text will be represented by its bounding rectangle:
interfaceHighlightArea{
height: number;
left: number;
pageIndex: number;
top: number;
width: number;
}
The `top` and `left` properties are the distances from the top-left corner of the bounding rectangle to the top and left side of pages.
While `height` and `width` properties are the height and width of the rectangle. All those properties are the number of percentages, not the absolute values in pixels.
The `pageIndex` property is the index of page that the selected text belongs to.
The `HighlightArea` interface is available as following
Each page of the document consists of different layers including canvas, annotations, texts. The text layer is a `div` element that renders all texts in the page.
It is constructed by multiple `span` (or `div`) elements. In order to store exactly the text user selects, we need the `SelectionData` data structure:
interfaceSelectionData{
startPageIndex: number;
startOffset: number;
startDivIndex: number;
endPageIndex: number;
endOffset: number;
endDivIndex: number;
}
The `startPageIndex` is the index of starting page. The `startOffset` property is the index of starting character in the starting text element which is determined by the `startDivIndex` property.
The same definitions are used for the `endPageIndex`, `endOffset` and `endDivIndex` properties.
The `SelectionData` interface is available as following
After you select texts in the document, it will show a Add a note icon, but clicking it doesn't show up anything.
Don't worry about it. We are going to show a form for adding a note in the next section.
renderHighlightContent
This prop is used to render an element which is shown after user switches to the highlighting mode. Technically, it's called after the
`renderHighlightTarget`'s `toggle()` is invoked.
`renderHighlightContent` provides the following properties:
Property
Type
Description
From
`cancel()`
`Function`
Cancel the selection
2.3.0
`highlightAreas`
`HighlightArea[]`
The list of highlight areas
2.3.0
`selectedText`
`string`
The selected text
2.3.0
`selectionData`
`SelectionData`
The selection data
2.3.0
`selectionRegion`
`HighlightArea`
The area represents the entire selected region
2.3.0
To make it simple, we just show a textarea for user to enter the note's message:
Clicking the Add button will call the `addNote` function. In addition to track the current `message`, we also need to store
the list of notes, and the latest `id` for note.
In reality, you might store and load notes from the database. In our example, the note's `id` is generated manually.
The `sidebarTabs` propety defines the list of tabs that will be shown in the sidebar. It is a function taking the default tabs, and returns the list
of tabs.
In our specific example, the tab showing all notes is the last one.
Open the tab when clicking a highlight
As mentioned in the previous section, clicking a highlight area will jump to the associated note in the sidebar.
Since the tab listing all notes can be closed, we have to open the tab first to make sure the notes visible:
The `activateTab` function accepts the index of tab that you want to open. Our specific example has four tabs, and the tab showing
all notes is the last one.
The last but not least thing is do not forget to reset (or load new notes) when user opens new document.
We can do it by handling the `onDocumentLoad` event: