⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfviewport.java

📁 Java生成PDF Java生成PDF Java生成PDF
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            painters[pagenum].interrupt();        }        boolean isInterrupted() {            return interrupted;        }    }    /**     * This interface needs to be implemented by any class that wants to     * be notified of {@link PDFViewport} events. For a <code>ViewportObserver</code>     * to recieve events they need to add themselves as an observer.     *     * @see PDFViewport#addObserver(PDFViewport.ViewportObserver)     */    public interface ViewportObserver {        /**         * This event fires whenever the {@link PDFViewport} changes the page         * or the DPI.         *         * @param x the X co-ordinate of the upper-left hand corner of the viewport, in points         * @param y the Y co-ordinate of the upper-left hand corner of the viewport, in points         * @see PDFViewport         */        public void viewportChanged(double x, double y);        /**         * This event fires whenever the {@link PDFViewport#writeAsTIFF writeAsTIFF}         * event is completed.         * @see PDFViewport         */        public void writeAsTIFFComplete();    }    //----------------------------------------------------------------------------    // Methods relating to interaction follow    /**     * Set the event handler for any mouse clicks. The default handler     * allows some basic interaction with annotations - hyperlink processing     * and the like.     * @param handler the {@link EventHandler} that should be used to handle     * mouse or keyboard events in this viewport.     * @since 2.7.8     */    public void setEventHandler(EventHandler handler) {        if (handler!=null && handler.getViewport()!=null && handler.getViewport()!=this) {            throw new IllegalStateException("EventHandler already registered with a viewport");        }        boolean oldmouse = eventhandler==null ? false : (eventhandler.getType()&handler.TYPE_MOUSE)!=0;        boolean oldmotion = eventhandler==null ? false : (eventhandler.getType()&handler.TYPE_MOUSEMOTION)!=0;        boolean oldkey = eventhandler==null ? false : (eventhandler.getType()&handler.TYPE_KEYBOARD)!=0;        boolean newmouse = handler==null ? false : (handler.getType()&handler.TYPE_MOUSE)!=0;        boolean newmotion = handler==null ? false : (handler.getType()&handler.TYPE_MOUSEMOTION)!=0;        boolean newkey = handler==null ? false : (handler.getType()&handler.TYPE_KEYBOARD)!=0;        if (newmouse && !oldmouse) {            addMouseListener(this);        } else if (!newmouse && oldmouse) {            removeMouseListener(this);        }        if (newmotion && !oldmotion) {            addMouseMotionListener(this);        } else if (!newmotion && oldmotion) {            removeMouseMotionListener(this);        }        if (newkey && !oldkey) {            addKeyListener(this);        } else if (!newkey && oldkey) {            removeKeyListener(this);        }        if (eventhandler!=null) eventhandler.setViewport(null);        eventhandler = handler;        if (eventhandler!=null) {            eventhandler.setViewport(this);            if (pagenum!=-1) {                eventhandler.pageDisplayed(new PDFNavigationEvent(this, pdf.getPage(pagenum)));            }        }    }    /**     * Run the specified action on the PDF. Currently only the "GoTo" actions are     * implemented, plus a few basic Named actions.     * @param action the Action to run.     * @return true if the action was recognised and run successfully, false otherwise.     * @since 2.7.7     */    public boolean runAction(PDFAction action) {        boolean success = false;        while (action!=null) {            success = true;            PDFPage page = null;            float x = Float.NaN, y = Float.NaN, zoom = 0;            String type = action.getType();            if (type.equals("GoToFit")) {                page = action.getPage();                zoom = Math.min((float)getWidth()/page.getWidth(), (float)getHeight()/page.getHeight());            } else if (type.equals("GoTo")) {                page = action.getPage();                float[] coords = action.getGoToCoordinates();                x = coords[0];                y = coords[1];                zoom = coords[2];            } else if (type.equals("GoToFitWidth")) {                page = action.getPage();                float[] coords = action.getGoToCoordinates();                zoom = (float)getWidth()/page.getWidth();                y = coords[0];            } else if (type.equals("GoToFitHeight")) {                page = action.getPage();                float[] coords = action.getGoToCoordinates();                zoom = (float)getHeight()/page.getHeight();                x = coords[0];            } else if (type.equals("GoToFitRectangle")) {                page = action.getPage();                float[] coords = action.getGoToCoordinates();                x = Math.min(coords[0], coords[2]);                y = Math.min(coords[1], coords[3]);                zoom = Math.min(getWidth() / Math.abs(coords[2]-coords[0]), getHeight() / Math.abs(coords[3]-coords[1]));            } else if (type.equals("Named:NextPage")) {                if (pagenum < pdf.getNumberOfPages()-2) {                    page = pdf.getPage(pagenum+1);                }            } else if (type.equals("Named:PrevPage")) {                if (pagenum > 0) {                    page = pdf.getPage(pagenum-1);                }            } else if (type.equals("Named:FirstPage")) {                page = pdf.getPage(0);            } else if (type.equals("Named:LastPage")) {                page = pdf.getPage(pdf.getNumberOfPages()-1);            } else if (type.equals("Named:Print")) {                print();            } else {                success = false;//                System.out.println("Action="+action);            }            if (page!=null) {                if (!pdf.getPages().contains(page)) {                    throw new IllegalArgumentException("Action refers to a page from another PDF");                }                setPageNumber(page.getPageNumber()-1, x, page.getHeight()-y, zoom*SCREENDPI);            }            action = action.getNext();        }        return success;    }    /**     * Return the annotation this mouse event was on. We iterate through each     * shape stored in the annotationmap (as created by storePageAnnotation), and     * check if it contains the point. Returns null if no annotations match.     *     * Not very efficient, should probably store these as a B-Tree.     */    PDFAnnotation getAnnotation(PDFMouseEvent event) {        PDFPage page = event.getPage();        Map hotspots = (Map)annotationmap.get(page);        if (hotspots!=null) {            Point2D point = event.getPagePoint();            for (Iterator i = hotspots.entrySet().iterator();i.hasNext();) {                Map.Entry e = (Map.Entry)i.next();                Shape s = (Shape)e.getKey();                if (s.contains(point)) {                    return (PDFAnnotation)e.getValue();                }            }        }        return null;    }    /**     * Store the shapes for each annotation on the specified page in the "annotationsmap"     * map. The key is a Shape, the value a PDFAnnotation     */    private void storePageAnnotations(PDFPage page) {        Map map = (Map)annotationmap.get(page);        if (map==null) {            map = new HashMap();            annotationmap.put(page, map);        }        for (Iterator i = page.getAnnotations().iterator();i.hasNext();) {            PDFAnnotation annot = (PDFAnnotation)i.next();            Shape shape = null;            if (!(annot instanceof AnnotationMarkup) && !(annot.getType().equals("Popup"))) {                if (annot instanceof AnnotationLink) {                    float[] c = ((AnnotationLink)annot).getCorners();                    if (c!=null) {                        GeneralPath p = new GeneralPath();                        p.moveTo(c[0], c[1]);                        p.lineTo(c[2], c[3]);                        p.lineTo(c[4], c[5]);                        p.lineTo(c[6], c[7]);                        p.closePath();                        shape = p;                    }                }                if (annot instanceof AnnotationNote && !((AnnotationNote)annot).isOpen()) {                    float[] c = annot.getRectangle();                    shape = new Rectangle2D.Float(c[0], c[1], 20, 20);                }                if (shape==null) {                    float[] c = annot.getRectangle();                    shape = new Rectangle2D.Float(c[0], c[1], c[2]-c[0], c[3]-c[1]);                }            }            if (shape!=null) {                map.put(shape, annot);            }        }    }    private void removePageAnnotations(PDFPage page) {        annotationmap.remove(page);    }    //----------------------------------------------------------------------    // Glue for event handling    /**     * Given a regular AWT MouseEvent, wrap it in a PDFMouseEvent and return. This     * only happens if the event occurred on a page - if not, return null.     */    private PDFMouseEvent getMouseEvent(MouseEvent event) {        int framex = pageImage==null ? 0 : pageImage.getWidth(this) < getWidth() ? (getWidth() - pageImage.getWidth(this)) / 2 : 0;        float pagex = (float)(((event.getX() - framex) / getDPI() * 72f) + x1);        int framey = pageImage==null ? 0 : pageImage.getHeight(this) < getHeight() ? (getHeight() - pageImage.getHeight(this)) / 2 : 0;        float pagey  = (float)(pageheight - y1 - ((event.getY()-framey) / getDPI() * 72f));        if (pagex >= 0 && pagex <= pagewidth && pagey>=0 && pagey <= pageheight) {            PDFPage page = pdf.getPage(pagenum);            return new PDFMouseEvent(event, this, page, pagex, pagey);        } else {            return null;        }    }    /**     * Given a regular AWT KeyEvent, wrap it in a KeyEvent and return.     */    private PDFKeyEvent getKeyEvent(KeyEvent event) {        return new PDFKeyEvent(event, this);    }    public void mouseEntered(MouseEvent event) {    }    public void mouseExited(MouseEvent event) {    }    public void mousePressed(MouseEvent event) {        PDFMouseEvent pdfe = getMouseEvent(event);        if (pdfe!=null) eventhandler.mousePressed(pdfe);    }    public void mouseReleased(MouseEvent event) {        PDFMouseEvent pdfe = getMouseEvent(event);        if (pdfe!=null) eventhandler.mouseReleased(pdfe);    }    public void mouseMoved(MouseEvent event) {        PDFMouseEvent pdfe = getMouseEvent(event);        if (pdfe!=null) eventhandler.mouseMoved(pdfe);    }    public void mouseDragged(MouseEvent event) {        PDFMouseEvent pdfe = getMouseEvent(event);        if (pdfe!=null) eventhandler.mouseDragged(pdfe);    }    public void mouseClicked(MouseEvent event) {        PDFMouseEvent pdfe = getMouseEvent(event);        if (pdfe!=null) eventhandler.mouseClicked(pdfe);    }    public void keyPressed(KeyEvent event) {        PDFKeyEvent pdfe = getKeyEvent(event);        if (pdfe!=null) eventhandler.keyPressed(pdfe);    }    public void keyReleased(KeyEvent event) {        PDFKeyEvent pdfe = getKeyEvent(event);        if (pdfe!=null) eventhandler.keyReleased(pdfe);    }    public void keyTyped(KeyEvent event) {        PDFKeyEvent pdfe = getKeyEvent(event);        if (pdfe!=null) eventhandler.keyTyped(pdfe);    }}

⌨️ 快捷键说明

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