Text highlight annotation by mouse event not picking up anything

WebViewer Version: 10.9.0 and 11.4.0

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? Yes

Please give a brief summary of your issue:
(Think of this as an email subject)
No annotations on mouse move event.

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)
I’m trying to place some text highlight annotations and access them as I hover the mouse pointer over them. I can reproduce this using your webviewer-react sample. Here’s the change to src/App.tsx.

import React, { useRef, useEffect } from "react";
import WebViewer, { Core } from "@pdftron/webviewer";
import "./App.css";

const App = () => {
  const viewer = useRef(null);

  useEffect(() => {
    WebViewer(
      {
        path: "/lib/webviewer",
        initialDoc: "/files/WebviewerDemoDoc.pdf",
        licenseKey: "your_license_key", // sign up to get a free trial key at https://dev.apryse.com
      },
      viewer.current!,
    ).then((instance) => {
      const { documentViewer, annotationManager, Annotations } = instance.Core;

      documentViewer.addEventListener("documentLoaded", () => {
        const doc = instance.Core.documentViewer.getDocument();
        doc.getTextPosition(1, 51, 68).then((quads) => {
          const annot = new Annotations.TextHighlightAnnotation();
          annot.setPageNumber(1);
          annot.IsHoverable = true;
          annot.Listable = true;
          annot.Quads.push(...(quads as Core.Math.Quad[]));

          annotationManager.addAnnotation(annot);
          annotationManager.redrawAnnotation(annot);
        });
      });

      const onMouseMove = (evt: MouseEvent) => {
        // This always returns `null`
        const annot = annotationManager.getAnnotationByMouseEvent(evt);
        console.log("hovering annotation", annot);
      };

      documentViewer.addEventListener("mouseMove", onMouseMove);
    });
  }, []);

  return (
    <div className="App">
      <div className="header">React sample</div>
      <div className="webviewer" ref={viewer}></div>
    </div>
  );
};

export default App;

This highlights the words “Important Factors” on the first page. When I hover the mouse over them, annotationManager.getAnnotationByMouseEvent keeps returning null. So in the console, I only see got annotation null logged over and over as I move the mouse over these annotations.

I’ve been looking through the other available properties but nothing jumps out at me so far. Any idea what I’m missing?

Please provide a link to a minimal sample where the issue is reproducible:
See above.

1 Like

Hello, I’m AiRon, an automated tech support bot :robot:
While you wait for one of our customer support representatives, I can help you with your question.


It seems like you’re trying to detect when your mouse is hovering over a highlight annotation in Apryse’s WebViewer but are not receiving the expected results. The method annotationManager.getAnnotationByMouseEvent(evt) should return the annotation you are hovering over if it is correctly configured.

Some things you can try:

  • Ensure that the highlight annotation is correctly added and redrawn on the page. Verify that the annotation’s quads are correctly set and correspond to the text you are targeting.

  • Check that the mouse event is correctly capturing the coordinates of the annotations. Sometimes, CSS styling can affect the bounding box of the viewer, which might cause issues with detecting mouse events.

  • Make sure that the annotation is selectable and interactive. This can be configured using properties such as annot.NoRotate = false; and annot.NoMove = false; to ensure that the annotation is interactive.

  • Verify that the onMouseMove event is correctly attached to the documentViewer and that the event is being fired as expected.



Here are some resources that might help you:


Hopefully, this helps you solve your problem while you wait for a human to get back to you.

1 Like

Ok I think I found the problem. Though admittedly I don’t understand why this is the case. Pushing the quads to the existing value on the annotation object seems to omit recalculation of the underlying rectangle.

annot.Quads.push(...(quads as Core.Math.Quad[]));
console.log(annot.getRect()); // Object { x1: 0, y1: 0, x2: 0, y2: 0 }

But assigning the array of quads seems to do the right thing.

annot.Quads = quads as Core.Math.Quad[];
console.log(annot.getRect()); // Object { x1: 107.312, y1: 481.199, x2: 541.7959999999999, y2: 552.6949999999999 }

This finally makes .getAnnotationByMouseEvent correctly return the annotations as I move the mouse pointer over them. I’m guessing this is because the correct rectangle matches the coordinates in the mouse event. Whereas before, it’s as if the annotations only take up a single point in the top left corner.

I can’t imagine this is intended behavior so I hope your team would consider this a bug. No blocker on my end though since I’ll just stick to the assignment approach.

1 Like

Hello @jamesl,

Thank you for the update and your investigation.

This is actually expected behavior. With how our getters and setters are set up, we support assigning these properties, and recommend customers use assignment when updating them. See our guides here for examples on our recommended approach for creating Text Highlight Annotations programmatically: https://docs.apryse.com/web/guides/annotation/types/highlightannotation#instantiation

Best Regards,
Jacob Romano Carlsen
Web Development Support Engineer
Apryse Software Inc.

1 Like

I see. Yea I guess pushing to the array directly wouldn’t give you the same hook that the setter would allow. Thanks for the follow up.

2 Likes