The PdfHighlighter component is the primary entry point for integrating PDF highlighting capabilities into a React application. It requires a pdfDocument (from pdfjs-dist) and a set of callback props to handle user interactions like text selection, area selection, and scrolling.
Key Props
pdfDocument: The PDFDocumentProxy instance.highlights: An array of highlight objects (extending IHighlight).highlightTransform: A function used to render the UI for each highlight. It provides access to tools like viewportToScaled, screenshot, and setTip.onSelectionFinished: A callback triggered when a user finishes selecting text or an area. It provides the scaledPosition, content (text or image), and functions to hide the selection or transform it into a 'ghost' highlight.scrollRef: A callback that provides a scrollTo function, allowing you to programmatically navigate to specific highlights.onScrollChange: A callback triggered when the PDF viewer scrolls.pdfScaleValue: Controls the PDF zoom level (defaults to "auto").enableAreaSelection: An optional function to enable non-text (area) selection. It receives the MouseEvent and should return true if selection should proceed.pdfViewerOptions: Optional configuration passed directly to the underlying pdfjs-dist PDFViewer.
Example Usage
<PdfHighlighter
pdfDocument={myPdfDocument}
highlights={myHighlights}
highlightTransform={(highlight, index, setTip, hideTip, viewportToScaled, screenshot, isScrolledTo) => (
<div key={highlight.id} onClick={() => setTip(highlight.position, <MyTipComponent />)}>
{/* Custom highlight UI */}
</div>
)}
onSelectionFinished={(position, content, hideTipAndSelection, transformSelection) => {
console.log('New selection:', position, content);
return <MySelectionMenu onSave={...} />;
}}
scrollRef={(scrollTo) => {
// Use scrollTo(highlight) to navigate
}}
onScrollChange={() => console.log('scrolled')}
/>