JavaScript: Copy text, which is selected in web viewer, to the clipboard.

Code samples for VintaSoft Imaging .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 2305
Joined: Thu Jul 10, 2008 2:21 pm

JavaScript: Copy text, which is selected in web viewer, to the clipboard.

Post by Alex »

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:

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.");
    }
}
Post Reply