JavaScript: Copy text, which is selected in web viewer, to the clipboard.
Posted: Wed May 17, 2023 9:01 am
Web image viewer allows to open document (PDF, DOCX, XLSX) and select text in viewer using mouse. By default the viewer does not handle pressing of Ctrl+C key and selected text cannot be copied to the clipboard by pressing Ctrl+C key. If selected text must be copied to the clipboard by pressing Ctrl+C key, this can be done with custom code.
Here is JavaScript code snippet that allows to copy text, which is selected in web viewer, to the clipboard by pressing Ctrl+C key:
Here is JavaScript code snippet that allows to copy text, which is selected in web viewer, to the clipboard by pressing Ctrl+C key:
Code: Select all
// subscribe to the keyup event of document
document.onkeyup = function (event) {
// if Ctrl+C key is pressed
if (event.ctrlKey && event.key === "c") {
// get the text selection tool, which is used by web document viewer
var textSelectionTool = _docViewer.getVisualToolById("TextSelectionTool");
// get selected text
var selectedText = textSelectionTool.get_SelectedText();
// copy text to the clipboard
navigator.clipboard.writeText(selectedText);
// show alert
alert("Text is copied to the clipboard.");
}
}