texthighlighter.java

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

JAVA
158
字号
// $Id: TextHighlighter.java,v 1.3 2007/09/12 10:04:59 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.*;import java.util.*;/** * A feature that allows the highlighting of text in the viewer. * The name of this feature is "TextHighlighter". * @since 2.8.1 */public class TextHighlighter extends ViewerFeature implements DocumentPanelListener, PagePanelListener{    private String[] words;    private PDFPage page;    private Paint color;    private Stroke stroke;    private float margin;    private int type;    /**     * A parameter to {@link #setHighlightType setHighlightType()} that will cause the highlight to be filled.     * An example would be <code>setHighlightType(TYPE_FILL, new Color(0x70FFFF00, true), null, 0)</code>,     * to highlight the text with a translucent yellow (the default).     */    public static final int TYPE_FILL = 0;    /**     * A parameter to {@link #setHighlightType setHighlightType()} that will cause the highlight to be outlined.     * An example would be <code>setHighlightType(TYPE_OUTLINE, Color.red, new BasicStroke(), 2)</code> to draw     * a solid red outline around the text with a 2 point margin     */    public static final int TYPE_OUTLINE = 1;    /**     * A parameter to {@link #setHighlightType setHighlightType()} that will cause the highlight to be outlined.     * An example would be <code>setHighlightType(TYPE_UNDERLINE, Color.red, new BasicStroke(), 2)</code> to draw     * a solid red underline two points below the text.     */    public static final int TYPE_UNDERLINE = 2;    /**     * Create a new TextHighlighter     */    public TextHighlighter() {        super("TextHighlighter");        words = new String[0];        setHighlightType(TYPE_FILL, new Color(0x70FFFF00, true), null, 0);    }    /**     * Add a word to highlight to this TextHighlighter.     * @param word the word to highlight if found     */    public void addWord(String word) {        if (word!=null && word.length()>0 && !Arrays.asList(words).contains(word)) {            String[] t = new String[words.length+1];            System.arraycopy(words, 0, t, 0, words.length);            t[t.length-1] = word;            words = t;        }    }    public void initialize(PDFViewer viewer) {        super.initialize(viewer);        viewer.addDocumentPanelListener(this);    }    public void documentUpdated(DocumentPanelEvent event) {        if (event.getType()=="viewportChanged") {            event.getDocumentPanel().getViewport().addPagePanelListener(this);        }    }    /**     * Set the type of Highlight to use     * @param type one of {@link #TYPE_FILL}, {@link #TYPE_OUTLINE} or {@link #TYPE_UNDERLINE}     * @param color the color to use - must not be null     * @param stroke the stroke to use for outline or underlining. May be null.     * @param margin how many points around the text to use as a margin     */    public void setHighlightType(int type, Paint color, Stroke stroke, float margin) {        if (color==null) throw new IllegalArgumentException("color is null");        this.type = type;        this.color = color;        this.stroke = stroke;        this.margin = margin;    }    /**     * Draw the highlight. This method may be overridden by subclasses to highlight     * the text in a different way     * @param g the Graphics object to paint to     * @param width the width of the object     * @param height the height of the object     */    public void drawHighlight(Graphics2D g, int width, int height) {        g.setPaint(color);        if (type==TYPE_OUTLINE) {            if (stroke!=null) g.setStroke(stroke);            g.drawRect(0, 0, width, height);        } else if (type==TYPE_UNDERLINE) {            if (stroke!=null) g.setStroke(stroke);            g.drawLine(0, height, width, height);        } else {            g.fillRect(0, 0, width, height);        }    }    public void pageUpdated(PagePanelEvent event) {        if (event.getType()=="redrawing") {            event.getPagePanel().setExtractText(true);        } else if (event.getType()=="redrawn" && words.length>0) {            PagePanel panel = event.getPagePanel();            if (panel.getPage()!=page) {                for (Iterator i = panel.getPageExtractor().getMatchingText(words).iterator();i.hasNext();) {                    PageExtractor.Text text = (PageExtractor.Text)i.next();                    JPanel highlight = new JPanel() {                        public void paintComponent(Graphics g) {                            drawHighlight((Graphics2D)g, getWidth(), getHeight());                        }                    };                    float[] c = text.getCorners();                    float[] r = new float[] { c[0]-margin, c[1]-margin, c[4]+margin, c[5]+margin }; // Assume rectangular                    AnnotationComponentFactory.bindComponentLocation(highlight, r);                    panel.add(highlight);                    highlight.setVisible(true);                }                page = panel.getPage();            }        }    }    /*    public static void main(String[] args) {        if (args.length<1) {            System.out.println("Usage: java TextHighlighter file.pdf word\n");        }        final java.io.File ffile = new java.io.File(args[0]);        TextHighlighter hl = new TextHighlighter();        hl.addWord(args[1]);        final Collection c = new ArrayList(ViewerFeature.getAllFeatures());        c.add(hl);        SwingUtilities.invokeLater(new Runnable() {            public void run() {                PDFViewer viewer = PDFViewer.newPDFViewer(c);                    if (ffile != null) viewer.loadPDF(ffile);                }        });    }    */}

⌨️ 快捷键说明

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