annotationadd.java
来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 113 行
JAVA
113 行
// $Id: AnnotationAdd.java,v 1.4 2007/11/08 09:44:18 mike Exp $package org.faceless.pdf2.viewer2.feature;import org.faceless.pdf2.viewer2.*;import org.faceless.pdf2.*;import java.awt.*;import java.awt.geom.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;/** * A {@link AbstractRegionSelector} that allows new annotations to be added to the * PDF Page. * <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 AnnotationAdd extends AbstractRegionSelector{ public AnnotationAdd() { super("AnnotationAdd"); setButton("Edit", "resources/icons/note_add.png", "tt.AnnotationAdd"); } public void action(PagePanel panel, Point2D start, Point2D end) { PDFPage page = panel.getPage(); float[] box = page.getBox("ViewBox"); Rectangle2D rect = new Rectangle2D.Float(box[0], box[1], box[2]-box[0], box[3]-box[1]); if (rect.contains(start) && rect.contains(end)) { PDFAnnotation annot = displayDialog(panel.getDocumentPanel().getViewer()); if (annot!=null) { float x1 = (float)Math.min(start.getX(), end.getX()); float y1 = (float)Math.min(start.getY(), end.getY()); float x2 = (float)Math.max(start.getX(), end.getX()); float y2 = (float)Math.max(start.getY(), end.getY()); annot.setRectangle(x1, y1, x2, y2); annot.setPage(panel.getPage()); panel.redrawAnnotation(annot); } } } /** * Display a dialog to create a new PDFAnnotation, and return it if OK or null if Cancelled. */ private PDFAnnotation displayDialog(final PDFViewer root) { Window window = JOptionPane.getFrameForComponent(root); final JDialog dialog; if (window instanceof Frame) { dialog = new JDialog((Frame)window, SuperJOptionPane.getLocalizedString("Annotation"), true); } else { dialog = new JDialog((Dialog)window, SuperJOptionPane.getLocalizedString("Annotation"), true); } final JPanel body = new JPanel(new BorderLayout()) ; final PDFAnnotation[] annotationholder = new PDFAnnotation[1]; final JList list = new JList(new String[] { "Note" }); list.setMinimumSize(new Dimension(70, 10)); final JSplitPane splitpane = new JSplitPane(); splitpane.setTopComponent(list); splitpane.setBottomComponent(new JPanel()); splitpane.getBottomComponent().setPreferredSize(new Dimension(600, 250)); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { String key = list.getSelectedIndex()==-1 ? null : (String)list.getSelectedValue(); if ("Note".equals(key)) { AnnotationNote note = new AnnotationNote(); note.setAuthor(root.getPropertyManager().getProperty("user.name")); splitpane.setBottomComponent(AnnotationNoteFactory.createEditPanel(note, false)); annotationholder[0] = note; } else { annotationholder[0] = null; splitpane.setBottomComponent(new JPanel()); } } }); body.add(splitpane, BorderLayout.CENTER, 0); final JPanel buttonpane = new JPanel(); JButton cancelbutton = new JButton(SuperJOptionPane.getLocalizedString("Cancel")); cancelbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { annotationholder[0] = null; dialog.setVisible(false); dialog.dispose(); } }); JButton okbutton = new JButton(SuperJOptionPane.getLocalizedString("OK")); okbutton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { dialog.setVisible(false); dialog.dispose(); } }); buttonpane.add(cancelbutton); buttonpane.add(okbutton); body.add(buttonpane, BorderLayout.SOUTH); dialog.setContentPane(body); dialog.setResizable(true); dialog.pack(); dialog.setLocationRelativeTo(root); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { dialog.dispose(); } }); dialog.setVisible(true); ((JButton)getComponent()).setSelected(false); return annotationholder[0]; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?