Get the coordinate of a click in PDF

Hello, I would like to use pdf js to retrieve the coordinate of a click in a document.
I tried embedding the PDF in an Iframe but obviously it doesn’t work.
Would it be possible to develop this “simple” functionality in javascript with the free version? I tried on the demo page with my console, bu it does not seem to work…

I have neither the budget nor the need to upgrade.

Thanks for your help !

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi there,

In the future, please fill out the issue template correctly or your ticket will be closed with no reply.

I’m assuming you mean the coordinates relative to the document? If so, here’s some code:

  const { documentViewer } = instance.Core;

  const getMouseLocation = e => {
    const scrollElement = documentViewer.getScrollViewElement();
    const scrollLeft = scrollElement.scrollLeft || 0;
    const scrollTop = scrollElement.scrollTop || 0;
  
    return {
      x: e.pageX + scrollLeft,
      y: e.pageY + scrollTop
    };
  };

  
  instance.Core.documentViewer.addEventListener('click', (e) => {

    const windowCoordinates = getMouseLocation(e);
    const displayMode = documentViewer.getDisplayModeManager().getDisplayMode();

    // takes a start and end point but we just want to see where a single point is located
    const page = displayMode.getSelectedPages(windowCoordinates, windowCoordinates);
    const clickedPage = (page.first !== null) ? page.first : documentViewer.getCurrentPage();

    const pageCoordinates = displayMode.windowToPage(windowCoordinates, clickedPage);

    console.log(pageCoordinates)
  })

There are many different coordinate systems involved here (PDF coordinates differ from normal coordinates), and we also need to figure out which page they clicked on, which is why this looks so confusing.

Hope this helps,

Thanks,
Logan

1 Like