gotoactionhandler.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 108 行

JAVA
108
字号
// $Id: GoToActionHandler.java,v 1.9 2007/10/29 04:10:10 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import javax.swing.*;import java.awt.geom.*;import java.awt.*;/** * Create an action handler for "GoTo" actions and the named actions that move between * pages. * The name of this feature is "GoToActionHandler". * <p><i>This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.</i></p> * @since 2.8 */public class GoToActionHandler extends ActionHandler{    private static GoToActionHandler instance;        /**     * Return the GoToActionHandler     */    public static GoToActionHandler getInstance() {        if (instance==null) instance = new GoToActionHandler();        return instance;    }    private GoToActionHandler() {        super("GoToActionHandler");    }    public boolean matches(DocumentPanel panel, PDFAction action) {        String type = action.getType();        return type.equals("GoToFit") || type.equals("GoTo") || type.equals("GoToFitWidth") || type.equals("GoToFitHeight") || type.equals("GoToFitRectangle") || type.equals("Named:FirstPage") || type.equals("Named:PrevPage") || type.equals("Named:NextPage") || type.equals("Named:LastPage");    }    public void run(DocumentPanel panel, PDFAction action) {        PDF pdf = panel.getPDF();        int pagenumber = panel.getPageNumber();        PDFPage page = null;        float x = Float.NaN, y = Float.NaN, zoom = 0;        final int PAD = 4;        Rectangle2D fullsize = null;        Dimension avail = panel.getViewport().getViewportSize();        int dpi = panel.getToolkit().getScreenResolution();        String type = action.getType();        if (type.equals("GoTo")) {            page = action.getPage();            fullsize = PagePanel.getFullPageView(page);            float[] coords = action.getGoToCoordinates();            x = coords[0];            y = coords[1];            zoom = coords[2];        } else if (type.equals("GoToFit")) {            page = action.getPage();            fullsize = PagePanel.getFullPageView(page);            zoom = (float)Math.min((avail.getWidth()-PAD)/fullsize.getWidth(), (avail.getHeight()-PAD)/fullsize.getHeight()) / dpi * 72;        } else if (type.equals("GoToFitWidth")) {            page = action.getPage();            fullsize = PagePanel.getFullPageView(page);            float[] coords = action.getGoToCoordinates();            zoom = (float)((avail.getWidth()-PAD)/fullsize.getWidth()) / dpi * 72;            y = coords[0];        } else if (type.equals("GoToFitHeight")) {            page = action.getPage();            fullsize = PagePanel.getFullPageView(page);            float[] coords = action.getGoToCoordinates();            zoom = (float)((avail.getHeight()-PAD)/fullsize.getHeight()) / dpi * 72;            x = coords[0];        } else if (type.equals("GoToFitRectangle")) {            page = action.getPage();            fullsize = PagePanel.getFullPageView(page);            float[] coords = action.getGoToCoordinates();            x = Math.min(coords[0], coords[2]);            y = Math.min(coords[1], coords[3]);            zoom = (float)Math.min((avail.getWidth()-PAD) / Math.abs(coords[2]-coords[0]), (avail.getHeight()-PAD) / Math.abs(coords[3]-coords[1])) / dpi * 72;         } else if (type.equals("Named:NextPage")) {            if (pagenumber < pdf.getNumberOfPages()-1) page = pdf.getPage(pagenumber+1);        } else if (type.equals("Named:PrevPage")) {            if (pagenumber > 0) page = pdf.getPage(pagenumber-1);        } else if (type.equals("Named:FirstPage")) {            page = pdf.getPage(0);        } else if (type.equals("Named:LastPage")) {            page = pdf.getPage(pdf.getNumberOfPages()-1);        }        if (page!=null) {            if (!pdf.getPages().contains(page)) {                throw new IllegalArgumentException("Action refers to a page from another PDF");            }            if (x==x) {                x -= fullsize.getMinX();                if (x<0 || x>fullsize.getWidth()) x = Float.NaN;            }            if (y==y) {                y = (float)(fullsize.getMaxY()-y);                if (y<0 || y>fullsize.getHeight()) y = Float.NaN;            }            if (zoom<0 || zoom>64) {                zoom = Float.NaN;            }            panel.setPage(page, x, y, zoom);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?