Text selection in JavaScript PDF Viewer control
23 Oct 20254 minutes to read
The TextSelection module lets users highlight and copy text from the loaded PDF. Selection is enabled by default and can be configured or monitored programmatically to match application workflows.
Enable or disable text selection
Use the enableTextSelection property to enable or disable choosing text in the PDF Viewer.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Essential JS 2</title>
<!-- Essential JS 2 tailwind3 theme -->
<link href="https://cdn.syncfusion.com/ej2/31.2.2/tailwind3.css" rel="stylesheet" type="text/css"/>
<!-- Essential JS 2 PDF Viewer's global script -->
<script src="https://cdn.syncfusion.com/ej2/31.2.2/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id='container'>
<div id='pdfViewer' style="height:500px;width:100%;">
</div>
</div>
</body>
</html>ej.pdfviewer.PdfViewer.Inject(
ej.pdfviewer.Toolbar, ej.pdfviewer.Magnification, ej.pdfviewer.Navigation, ej.pdfviewer.LinkAnnotation,
ej.pdfviewer.ThumbnailView, ej.pdfviewer.BookmarkView, ej.pdfviewer.TextSelection
);
var pdfviewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
enableTextSelection: true // Defaults to true
});
pdfviewer.appendTo('#PdfViewer');
// Disable text selection later if required
pdfviewer.enableTextSelection = false;ej.pdfviewer.PdfViewer.Inject(
ej.pdfviewer.Toolbar, ej.pdfviewer.Magnification, ej.pdfviewer.Navigation, ej.pdfviewer.LinkAnnotation,
ej.pdfviewer.ThumbnailView, ej.pdfviewer.BookmarkView, ej.pdfviewer.TextSelection
);
var pdfviewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
enableTextSelection: true // Defaults to true
});
pdfviewer.serviceUrl = 'https://document.syncfusion.com/web-services/pdf-viewer/api/pdfviewer/';
pdfviewer.appendTo('#PdfViewer');
// Toggle on demand
pdfviewer.enableTextSelection = false;Text selection events
Monitor user interaction with selection events to coordinate downstream actions such as showing tooltips, enabling context menus, or storing analytics.
textSelectionStart
The textSelectionStart event fires when a user begins selecting text. Use it to reset temporary UI, pause conflicting shortcuts, or capture the starting context.
- Event arguments:
TextSelectionStartEventArgssupplies details such aspageNumber,bounds, andselectionBehavior.
var viewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
textSelectionStart: function (args) {
// args.pageNumber, args.bounds provide the starting context
console.log('Selection started', args);
}
});
viewer.appendTo('#PdfViewer');textSelectionEnd
The textSelectionEnd event triggers after the selection is finalized. Use it to access the selected text, toggle contextual commands, or store telemetry.
- Event arguments:
TextSelectionEndEventArgsincludespageNumber,bounds,selectedText, andisSelectionCopied.
var viewer = new ej.pdfviewer.PdfViewer({
documentPath: 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf',
textSelectionEnd: function (args) {
// For example, automatically copy or show a custom menu
console.log('Selection ended', args);
}
});
viewer.appendTo('#PdfViewer');