Link
Skip to main content

How to get page coordinates from mouse events

To get the page coordinates from mouse events, you will need to create a custom mouse listener and add it as an external handler.

final Viewer viewer = new Viewer();
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "inputFile.pdf");

final SwingGUI gui = viewer.getSwingGUI();
final SwingMouseFunctionality listener = new SwingMouseSelector(gui.getPdfDecoder(), gui, gui.getValues(), gui.getCommand()) {
    @Override
    public void mouseReleased(final MouseEvent e) {
        final Point p = getCoordsOnPage(e.getX(), e.getY(), viewer.getPdfDecoder().getPageNumber());
    }
};

viewer.getPdfDecoder().addExternalHandler(listener, Options.CustomMouseHandler);

Java uses a top-left coordinate system whereas PDF uses a bottom-left system so you may need to vertically flip the output with an AffineTransform. To learn more about page coordinates in PDF files, check out this blog article.


Get started with JPedal in 3 steps

  1. Fill in the form to download the trial jar →
  2. Copy the code snippets as instructed on the next page
  3. Build your solution using our docs

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download