How to clear previous search results?

Which product are you using?
PDF.js Express Viewer

PDF.js Express Version

Detailed description of issue
There are two very related issues

  1. If I search something twice using the UI.searchText API, I get an error message on the console saying
searchText.js:107 TypeError: Cannot read properties of undefined (reading 'text')
    at e.zC (webviewer-core.min.js:1559:222)
    at e.wg (webviewer-core.min.js:1544:33)
    at r (webviewer-core.min.js:324:55)
  1. If the second search is for a different string that is not located in the pdf (i.e. no results found), the previous search result highlight is not cleared. How to clear older results each time a new search is initiated?

Expected behaviour

  1. There should be no error on the console for a repeated search.
  2. On initiation of a new search, the previous search result should be cleared.

Does your issue happen with every document, or just one?

Code snippet

var search = function(search_term) {
    // search_term = 'Annotate'
    console.log("Initiating search: " + search_term)
    pdfjs_instance.UI.searchText(search_term, {
    pdfjs_instance.UI.closeElements([ 'searchPanel' ]);


var searchListener = function(searchValue, options, results) {
    console.log(`CALLBACK: ${searchValue}.`)
    // console.log(options);
    console.log(`Time taken ${( - start_time) / 1000}s. Total results found ${results.length}`)

// for 1

//for 2

Hello there,

We are currently investigating this issue and will provide you with an update when we have more information.

Thank you.

Hello there,

I tried using the code you provided in the then method when instantiating WebViewer, but I didn’t receive any errors. Could you please provide any steps or configurations I may have missed along with the PDF you used?

Please try updating to 8.7.4 as well.

Thank you,

Hi Darian,
Two questions about your setup

  1. Are you using version 8.7.2 or 8.7.4?
  2. What happens when you search for something twice?
  3. Irrespective of the error message, I want to know how to clear previous search results (i.e. any highlights of the previous search result) without initiating a new search.

Hello Simar,

I am using 8.7.4 and I do not see any errors in the console when I run your code, it appears to simply just search multiple times.

You can clear previous search results by using the clearSearchResults API.


Best Regards,