react-pdf-highlighter icon indicating copy to clipboard operation
react-pdf-highlighter copied to clipboard

Pre-Highlight using Search

Open Avinash-Cognizer opened this issue 2 years ago • 7 comments

Hello, How can we highlight word/section using search ? Right now the only way to Highlight any text is when you know its coordinates. what If I am given some word or sentences, let's say via an api, and I want to highlight it when the PDF renders is there any work-around for this ?

Avinash-Cognizer avatar Mar 04 '22 08:03 Avinash-Cognizer

Hey @agentcooper can you shed some light ?

Avinash-Cognizer avatar Mar 08 '22 06:03 Avinash-Cognizer

My team has been hacking on this from the parent component, using window.PDFViewer; beyond being kind of gross in using a global intended for debug, I believe it's the cause of infrequent crashes when our users search (I'm sure swapping out the viewer is not healthy).

  getFindController = () => window?.PdfViewer?.viewer?.findController;

  search = (searchValue) => {
    if (window.PdfViewer?.viewer) {
      if (!this.getFindController()) {
        // We re-create the PDFViewer bc the PDFFindController needs to be initialized here
        window.PdfViewer.viewer = new PDFViewer({
          container: window.PdfViewer.containerNode,
          eventBus: window.PdfViewer.eventBus,
          enhanceTextSelection: true,
          removePageBorders: true,
          linkService: window.PdfViewer.linkService,
          findController: new PDFFindController({ eventBus: window.PdfViewer.eventBus, linkService: window.PdfViewer.linkService }),
        });
        window.PdfViewer.eventBus.on('updatetextlayermatches', this.calculateMatchProgress);
        window.PdfViewer.init();
      }
      const findController = this.getFindController();
      findController.executeCommand('find', {
        query: searchValue,
        highlightAll: true,
        phraseSearch: true,
      });
      this.setState({ searchValue });
    }
  }

I would love to move this internal to PDFHighlighter, using a search prop (default to undefined = no search to run).

holub008 avatar Apr 07 '22 22:04 holub008

Hi @holub008 , I am able to add PDFFindController . thanks for sharing.

Also I am looking to have page navigation (previous/next button to navigation between pages) as well if any idea please share how I can achieve in react-pdf-highlighter.

qaisershehzad avatar Apr 19 '22 08:04 qaisershehzad

My team has been hacking on this from the parent component, using window.PDFViewer; beyond being kind of gross in using a global intended for debug, I believe it's the cause of infrequent crashes when our users search (I'm sure swapping out the viewer is not healthy).

  getFindController = () => window?.PdfViewer?.viewer?.findController;

  search = (searchValue) => {
    if (window.PdfViewer?.viewer) {
      if (!this.getFindController()) {
        // We re-create the PDFViewer bc the PDFFindController needs to be initialized here
        window.PdfViewer.viewer = new PDFViewer({
          container: window.PdfViewer.containerNode,
          eventBus: window.PdfViewer.eventBus,
          enhanceTextSelection: true,
          removePageBorders: true,
          linkService: window.PdfViewer.linkService,
          findController: new PDFFindController({ eventBus: window.PdfViewer.eventBus, linkService: window.PdfViewer.linkService }),
        });
        window.PdfViewer.eventBus.on('updatetextlayermatches', this.calculateMatchProgress);
        window.PdfViewer.init();
      }
      const findController = this.getFindController();
      findController.executeCommand('find', {
        query: searchValue,
        highlightAll: true,
        phraseSearch: true,
      });
      this.setState({ searchValue });
    }
  }

I would love to move this internal to PDFHighlighter, using a search prop (default to undefined = no search to run).

Hi could you show a more complete code snippet? I'm having issues when I try to run the executeCommand on the findcontroller

Thanks!

Daan-Grashoff avatar Jul 10 '22 19:07 Daan-Grashoff

Our team opened a PR here: https://github.com/agentcooper/react-pdf-highlighter/pull/185. We package the fork, which includes some other QOL goodies, on NPM: https://www.npmjs.com/package/@darian-lp/react-pdf-highlighter

It's our goal to move off the fork once the PRs are merged in, but we couldn't wait for search/zoom/rotate (standard functions on any PDF viewer) to be implemented.

holub008 avatar Jul 10 '22 20:07 holub008

Awesome! Thanks, I'm gonna check it out!

Daan-Grashoff avatar Jul 10 '22 21:07 Daan-Grashoff

I just wanted to say a MASSIVE thanks to @holub008 and his team for getting this implemented in a way more dev friendly way!!!

RutgerSwirski avatar Aug 22 '22 13:08 RutgerSwirski