posted on November 21, 2018

Hello,

While working with the WebLink PDF Viewer I found that the Ctrl+F shortcut only works after the PDF Viewer window has been selected.

For example, when the page first loads, the shortcut will open the browser's search/find tool, which cannot search text within a PDF document. As a result, the user must click somewhere in the PDF viewer before the shortcut will open the in-document search tool.

Ideally, the shortcut would work as soon as the PDF Viewer is loaded in the frame so that the additional click is not required and the user experience is more streamlined.

I implemented a workaround to add this functionality, but I thought it might be something worth adding to the official release for a slight improvement to usability.

 

For reference, the following is my workaround

This code was added to a script tag at the end of the DocView.aspx file

    // global flag
    var isCtrl = false;

    // function called by pdf viewer frame
    function hotkeyOverride(){
        // action on key up
        $(document).keyup(function(e) {
            if(e.which == 17) {
                // reset control key flag
                isCtrl = false;
            }
        });

        // action on key down
        $(document).keydown(function(e) {
            if(e.which == 17) {
                // set control key flag
                isCtrl = true;
            }
            // Ctrl + F for text search
            if(e.which == 70 && isCtrl) {
                // prevent default action and select in-document search
                e.preventDefault();
               
                // open the find tool if it is not currently visible
                $('#pdfViewerIFrame').contents().find('#viewFind:not(.toggled)').click();
            }
        });
    }

And this code was added to a script tag at the end of the PdfViewer.aspx file

    window.onload = function(){
      // if window is in iframe
      if(window.top != window.self){
        try{
          // call parent window function to override default hotkeys
          parent.hotkeyOverride();
        }
        catch(e){
          // log error
          console.log('Error: Parent function not found');
        }
      }
    };

The general idea is that when the PDF Viewer finishes loading in the iframe of the Document Viewer, it triggers the "hotkey override" function to allow Ctrl+F to work immediately without requiring the user to click on the document.

Note that this workaround was built with Windows in mind, so it could cause unexpected behavior on other operating systems/devices.

1 0