Get elements highlighted from the search result #18946
-
Problem statementI am trying to find all the occurrence of a word from a PDF document using pdf.js. After finding those words, I would like to add an annotation box (or a simple box for now) over those text. ProgressI have been able to highlight all the occurrences of the word/phrases looking into the examples present in pdf.js from this link: https://github.com/mozilla/pdf.js/tree/master/examples/components Here is the updated code:
I am now trying to get the div's or component of the matched words so that I can draw boxes over them. I looked into pdf_viewer.mjs and found an event named "updatetextlayermatches" hoping it might provide me components/div of the matched words but it provides an array of numbers which I did not quite understand. Can someone help and guide me in the right direction? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Let me share my simple work around for this. While inspecting the div, I noticed the highlighted text had class named "highlight" to them. So what I did was obtain all those elements with the 'highlight' class. Then, I obtained the position of each of them via getBoundingRect, created a Here is a small code snippet to show the main idea /* id is just a unique identification for those annotations. selectRect is the highlighted span element obtained when looping through the result of getElementsByClassName */
renderToViewport = (id, selectionRect) => {
const boundingClientRect = selectionRect.getBoundingClientRect();
const rectangle = document.createElement('section'); // creating a section element to append to the annotation layer
// get the page index of selected text
const pageIndex = this.getPageIndexOfSelectedText(selectionRect); // To obtain the page where this annotation is to be added
annotationHelper.appendAnnotation(pageIndex + 1);
const annotationLayer = document.getElementsByClassName("annotationLayer")[pageIndex]; // getting the annotation layer of that page
const pageEl = document.querySelector(`[data-page-number="${pageIndex + 1}"]`).getBoundingClientRect(); // parent element
const left = boundingClientRect.left - pageEl.left;
const top = boundingClientRect.top - pageEl.top;
rectangle.classList.add('freeTextAnnotation');
rectangle.classList.add('specimen-annotation');
rectangle.setAttribute("data-annotation-id", id)
rectangle.style.top = top + "px";
rectangle.style.left = left + "px";
rectangle.style.width = boundingClientRect.width + "px";
rectangle.style.height = boundingClientRect.height + "px";
rectangle.innerHTML = " SPECIMEN ";
rectangle.style.transformOrigin = `-${left}px -${top}px`;
annotationLayer.appendChild(rectangle);
} |
Beta Was this translation helpful? Give feedback.
Let me share my simple work around for this. While inspecting the div, I noticed the highlighted text had class named "highlight" to them. So what I did was obtain all those elements with the 'highlight' class. Then, I obtained the position of each of them via getBoundingRect, created a
Here is a small code snippet to show the main idea