Incomplete results from docViewer.textSearchInit when called in loadDocument(...).then

WebViewer Version: 7.3.4

Do you have an issue with a specific file(s)? No
Can you reproduce using one of our samples or online demos? Yes
Are you using the WebViewer server? No
Does the issue only happen on certain browsers? No
Is your issue related to a front-end framework? No
Is your issue related to annotations? No

I have functionality that searches for a list of terms in a document when the document is loaded to display the occurrences in a custom panel.

The code is triggered in when the loadDocument promise resolves.

I have found that this can provide incomplete results on my sample document ( 20 page pdf ) when the call is made immediately on loadDocument completing.

BUT if i call the countTerms after a delay: i.e. setTimeout(() => {countTerms();}, 2000);
then I get the correct number found for each term.

Is the document still being processed even though LoadDocument has resolved?
Is there another event I can hook into to trigger this logic?

Cut down version of code ( React based )

viewer.current
.loadDocument(url, {
extension: “pdf”,
useDownloader: false,
customHeaders: authHeader(),
})
.then(() => {
if (viewInstance.current) {
viewInstance.current.setFitMode(viewInstance.current.FitMode.FitWidth);
countTerms(); // this will do an api call to load the terms, then calls code to search for each of these terms as below…
}
})

const mode = getSearchMode(term, instance);

const results: ResultsMap = {};
let currPage = 0;
let pageOccurrence = 0;
const searchOptions = {
  // search of the entire document will be performed.
  fullSearch: true,
  startPage: 1,
  // The callback function that is called when the search returns a result.
  onResult: (result: any) => {
    if (result) {
      if (!results[term.term]) {
        results[term.term] = [];
      }
      if (currPage != result.pageNum) {
        pageOccurrence = 0;
        currPage = result.pageNum;
      }
      pageOccurrence++;
      results[term.term].push({
        term: result.resultStr,
        ambient: result.ambientStr,
        start: result.resultStrStart,
        end: result.resultStrEnd,
        pageNo: result.pageNum,
        searchResult: result,
        pageOccurrence: pageOccurrence,
      });
    }
    term.count++;
  },
  onDocumentEnd: () => {
    // add an entry if no results for this term so we know we've searched for it
    if (!results[term.term]) {
      results[term.term] = [];
    }
    resolve(results);
  },
};

instance.docViewer.textSearchInit(term.term, mode, searchOptions);

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:

Hello jim.bedworth,

loadDocument

We suggest waiting for the documentLoaded event.
Let me know if this works for you!

Best regards,
Tyler Gordon
Web Development Support Engineer
PDFTron

Hi

Thanks for the reply - but using the event also gives the same results.

i.e below is the term / count result calling immediately in documentLoaded or with 2 sec delay

from loaded event:
2018 count: 1
Access Skype count: 1
Authored count: 1
desktop count: 2
new collaboration count: 1
November count: 1
opening count: 1
permission count: 1
that count: 16

in event with delayed call by 2 secs:
2018 count: 1
Access Skype count: 3
Authored count: 1
clicking count: 5
desktop count: 9
get count: 1
Mark count: 1
new collaboration count: 1
November count: 1
opening count: 1
permission count: 1
that count: 19

Hello jim.bedworth,

You can also try finishedRendering: PDFTron WebViewer Class: DocumentViewer
This will ensure that the document has been fully loaded and rendered before you search.

Best regards,
Tyler Gordon
Web Development Support Engineer
PDFTron

Hi

Thanks for the suggestion - but did try that and found that if use the first time that is invoked i still get incomplete results… plus then have to have some complicated state as this gets called many time i.e. when panels displayed / searchresults etc

I have for now put the logic in the annotationsLoaded event as this seems to be called after the documentloaded and from initial testing it seems that the results I get from searching the whole document are complete.

Seems a bug though that documentLoaded is raised before the whole document is available for searching ?

Or perhaps another event is needed as something like “documentReadyForSearch” :wink:

Thanks

Hello jim.bedworth,

We have an event order chart found here: PDFTron

I think the possible reason annotationsLoaded event is working is if the document is being streamed, the annotationsLoaded event is the last event that occurs.

Best regards,
Tyler Gordon
Web Development Support Engineer
PDFTron