Skip to content

坐标

WebViewer 中原点 (0, 0) 是在 WebViewer 页面的左上角。x 轴水平向右延伸,y 轴垂直向下延伸。

将 PDF 文档坐标转换为 Window 坐标

javascript
const pageNumber = 1;
const pagePoint = {
    x: 0,
    y: 0
};

const windowPoint = docViewer.pageToWindow(pagePoint, pageNumber);
// { x: 47, y: 64 }

将 Window 坐标转换为 PDF 文档坐标

javascript
const pageNumber = 1;
const windowPoint = {
    x: 47,
    y: 64
};

const pagePoint = docViewer.windowToPage(windowPoint, pageNumber);
// { pageNumber: 1, x: 0, y: 0 }

将点击的点的坐标转换为 PDF 文档中的坐标

javascript
const getMouseLocation = e => {
  const scrollElement = docViewer.getScrollViewElement();
  const scrollLeft = scrollElement.scrollLeft || 0;
  const scrollTop = scrollElement.scrollTop || 0;

  return {
    x: e.pageX + scrollLeft,
    y: e.pageY + scrollTop
  };
}

const handleClick = (e) => {
  let windowCoordinates = getMouseLocation(e);
  const page = docViewer.getSelectedPage(windowCoordinates, windowCoordinates);

  const clickPageNumber = (page.first !== null) ? page.first : docViewer.getCurrentPage();
  const pagePoint = docViewer.windowToPage(windowCoordinates, clickPageNumber);
}

docViewer.addEvent('click', handleClick);