首页 > 解决方案 > 如何将 pdftron 注释添加到当前鼠标位置

问题描述

我正在使用 pdftron 编写一个 pdf 阅读软件。我知道docViewer.on('mouseLeftDown', e => {}可以获取事件,但onClick似乎无法获取鼠标事件。有什么好的解决办法吗?谢谢你。

      WebViewer(
        {
          path,
          initialDoc: "",
        },
        viewer.value
      ).then(function (instance) {
        const { Annotations, annotManager, docViewer } = instance;
        instance.contextMenuPopup.add({
          type: "actionButton",
          label: "MD",
          onClick: () => {
            const freeText = new Annotations.FreeTextAnnotation();
            freeText.PageNumber = docViewer.getCurrentPage();
            freeText.X=?;// I don't know how to set the freeText.X at the location of the mouse
            freeText.Y=?;
            freeText.Width = 150;
            freeText.Height = 50;

创建注释自由文本

鼠标位置和窗口坐标之间的转换

标签: javascriptvue.jspdftron

解决方案


我认为您可以这样做的一种方法是侦听 iframe 文档上的 contextmenu 事件,然后从那里存储最后一个鼠标事件,然后您可以在按钮的 onClick 中使用该事件。

例如

let lastContextMenuEvent;

instance.iframeWindow.document.addEventListener('contextmenu', (e) => {
  lastContextMenuEvent = e;
});

instance.contextMenuPopup.add({
  onClick: () => {
     // use lastContextMenuEvent here
  }
});

推荐阅读