Do you have keyword search listener?

Which product are you using?
PDF.js Express Viewer

PDF.js Express Version

Detailed description of issue
We are using PDF.js Express Viewer to view document. In Search option, is there any listeners to get the search keyword and search result count?

Expected behaviour
Get search result key word and search result total count.

Does your issue happen with every document, or just one?
This is not a issue.

Link to document
{Provide a link to the document in question if possible}

Code snippet
{Provide a relevant code snippet}

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi There,

Thanks for your question about the Search feature.

Below is a code snippet with the API you can use to get the search keyword and the result count.

Webviewer({
  ...options
}, document.getElementById('viewer')).then(async (instance) => {
  
  // Event to get the search keyword
  function searchListener(searchValue, options, results) {
      // The search value will be the search keyword
      console.log(searchValue, options, results);
      // To get the result count
      console.log(results.length)
  };

  instance.UI.addSearchListener(searchListener);
});

I hope this helps and please let me know if this works for you.

Thanks,

Dandara

Hi,

The above code only returns the results, if the search keyword matches with with text or lines inside the pdf document.
I want to catch the searched keyword, when there are no matches found for that searched keyword in the whole pdf document.

Please provide the solution for the same.

Regards,
Mandar

Hi There,

Thanks for your question. I updated the solution on this ticket and for your case, you can do as following

Webviewer({
  ...options
}, document.getElementById('viewer')).then(async (instance) => {
  
   function searchListener(searchValue, options, results) {
      if(results.length === 0) {
        console.log(searchValue);
      }
    };
  
    instance.UI.addSearchListener(searchListener);
});

Here is the documentation for this API.
Let me know if this works for you.

Thanks,
Dandara

Hi,
The count of result length keep on changing in console if the pdf document is too big, I think it takes time for big documents to return result length count so the count keep on updating in console. If possible can you please add some code to calculate all of its search length first and then return its total result length count.

Also is there any way till the matching searched values are getting fetched, we can block user for searching new keyword value ?
Please advise solution for the same.

Below is the previous code that you provided.
// Event to get the search result count. OBS: getting the count only after the search finishes

Core.documentViewer.addEventListener(‘searchInProgress’, (isInProgress) => {

if(isInProgress === false) {

  Core.documentViewer.addEventListener('searchResultsChanged', (result) => {

    console.log(`Result length: ${result.length}`);

  });

}

});

Hi,
The below code which you gave previously is working in printing the searched keyword when match result is zero but I am not able to store the searchValue in my local variables.

function searchListener(searchValue, options, results) {
// The search value will be the search keyword
console.log(searchValue, options, results);
// To get the result count
console.log(results.length)
};

instance.UI.addSearchListener(searchListener);

when I try to store value in below method then it is gives me undefined error.
function searchListener(searchValue, options, results) {

      if(results.length === 0) {

        console.log("length///",searchValue);

        var a = searchValue;

        console.log("length///'''''''",a);

        this.pdfKeywordSearch = a; or this.pdfKeywordSearch = searchValue

Error -
webviewer-ui.min.js:68 TypeError: Cannot set properties of undefined (setting ‘pdfKeywordSearch’)
at main.js:1:1682972
at webviewer-ui.min.js:68:839792
at Array.forEach ()
at a (webviewer-ui.min.js:68:839767)
at b.o (webviewer-ui.min.js:68:839841)
at webviewer-core.min.js:496:86
at Array.forEach ()
at b.r.trigger (webviewer-core.min.js:496:53)
at b.Ga.Vp (webviewer-core.min.js:314:374)
at hb (webviewer-core.min.js:316:402)

Do we have any different structure data format of searchValue due to which it is not allowing me to store variable in my local variable ?
Request you to please advise solution for the same.

Hi There,

Could you please provide me the big PDF file that you’re using to test the search so I can check better the case you said the resulting length keeps changing? Are you using the solution that I updated?

About the undefined error, it is saying that your property this is not defined, so you cannot set the property pdfKeywordSearch on it.

Thanks,

Dandara

Hi,
The pdf documents are confidential, I cannot send you.
I just need solution of this scenario ===> Till the search keyword is been searched, we have loader/spinner loading the count of match results, so till the loader is on, we can block the user from entering value for search keyword in search bar?

Please advise.