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