# Do you have keyword search listener?

**URL:** <https://pdfjs.community/t/do-you-have-keyword-search-listener/1549>\
**Category:** Technical Support\
**Tags:** pdfjs-express\
**Created:** [April 6, 2022, 1:51pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549 "2022-04-06T13:51:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yavula](https://avatars.discourse-cdn.com/v4/letter/y/58f4c7/32.png) [@yavula](https://pdfjs.community/u/yavula)\
**Post date:** [April 6, 2022, 1:51pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/1 "2022-04-06T13:51:24Z")

</div>

**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}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/ffbcd25c7d423db8d5acd5db201c272f4db306dd.png) [@system](https://pdfjs.community/u/system)\
**Post date:** [April 6, 2022, 1:51pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/3 "2022-04-06T13:51:29Z")

</div>

**Hello, I’m Ron, an automated tech support bot 🤖**

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

Guides:
- [PDF.js Express Viewer Overview - What is PDF.js Express Viewer?](https://pdfjs.express/documentation/get-started-viewer#what-is-pdf.js-express-viewer?)
- [PDF.js Express Viewer APIs](https://pdfjs.express/documentation/viewer/api)
- [Open a document, add annotations, download - 2. Listen for document load](https://pdfjs.express/documentation/use-case/upload-download.8#2.-listen-for-document-load)

APIs:
- [PDF.js Express Utilities](https://pdfjs.express/api/utils/index.html#__anchor)
- [ExpressUtils(optionsopt) - # async watermark(options) → {Promise.}](https://pdfjs.express/api/utils/ExpressUtils.html#watermark__anchor)
- [Adding a download button](https://pdfjs.express/api/utils/tutorial-Adding%20a%20download%20button.html#__anchor)

Forums:
- [PDF.js Viewer search function on mobile](https://pdfjs.community/t/pdf-js-viewer-search-function-on-mobile/1367/1)
- [Page transition issues in PDF.js viewer](https://pdfjs.community/t/page-transition-issues-in-pdf-js-viewer/1364/1)
- [Catch Mouse Over Events on Words](https://pdfjs.community/t/catch-mouse-over-events-on-words/1471/1)

---

<div class="post-metadata">

**Author:** ![dnavarro](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/dnavarro/32/707_2.png) [@dnavarro](https://pdfjs.community/u/dnavarro)\
**Post date:** [April 6, 2022, 4:50pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/4 "2022-04-06T16:50:22Z")

</div>

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.

```javascript
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

---

<div class="post-metadata">

**Author:** ![madalvi](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@madalvi](https://pdfjs.community/u/madalvi)\
**Post date:** [April 7, 2022, 10:40am UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/5 "2022-04-07T10:40:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dnavarro](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/dnavarro/32/707_2.png) [@dnavarro](https://pdfjs.community/u/dnavarro)\
**Post date:** [April 7, 2022, 4:55pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/6 "2022-04-07T16:55:57Z")

</div>

Hi There,

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

```javascript
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](https://pdfjs.express/api/UI.html#.searchListener__anchor) is the documentation for this API.  
Let me know if this works for you.

Thanks,  
Dandara

---

<div class="post-metadata">

**Author:** ![madalvi](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@madalvi](https://pdfjs.community/u/madalvi)\
**Post date:** [April 7, 2022, 7:12pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/7 "2022-04-07T19:12:06Z")

</div>

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}`);

  });

}

```

});

---

<div class="post-metadata">

**Author:** ![madalvi](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@madalvi](https://pdfjs.community/u/madalvi)\
**Post date:** [April 7, 2022, 10:02pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/8 "2022-04-07T22:02:07Z")

</div>

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.

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/pdfjscommunity/original/1X/0fdd2118b9e4e44e8ba25d4cd6604b0b2f2351f3.png)

---

<div class="post-metadata">

**Author:** ![dnavarro](https://yyz1.discourse-cdn.com/flex031/user_avatar/pdfjs.community/dnavarro/32/707_2.png) [@dnavarro](https://pdfjs.community/u/dnavarro)\
**Post date:** [April 7, 2022, 10:22pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/9 "2022-04-07T22:22:49Z")

</div>

> [@madalvi](#):
>
> Event to get the search result count. OBS: getting the count only after the search finishes

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

---

<div class="post-metadata">

**Author:** ![madalvi](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@madalvi](https://pdfjs.community/u/madalvi)\
**Post date:** [April 8, 2022, 4:32pm UTC](https://pdfjs.community/t/do-you-have-keyword-search-listener/1549/10 "2022-04-08T16:32:38Z")

</div>

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.
