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.


Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download