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

📄 pdfviewer.java

📁 Java生成PDF Java生成PDF Java生成PDF
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: PDFViewer.java,v 1.89 2007/05/21 15:02:19 mike Exp $package org.faceless.pdf2.viewer;import org.faceless.pdf2.*;import javax.swing.*;import javax.swing.event.*;import java.io.*;import java.awt.event.*;import java.awt.*;import javax.swing.filechooser.FileFilter;/** * The <code>PDFViewer</code> class is a simple Swing PDF viewer application. * It demonstrates the {@link PDFScrollPane} class, and can be run directly * from the JAR (via the {@link PDFTool} class) like so. * <pre> * java -jar bfopdf.jar <i>filename</i> * </pre> * The <i>filename</i> argument is optional, but if supplied will load the * specified PDF. * * <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.5 */public class PDFViewer extends JPanel{    private static final int INITWIDTH = 400;    private static final int INITHEIGHT = 200;    private static final String[] percentage = {"12.5%", "25%", "50%", "75%", "100%", "125%", "150%",                                           "200%", "400%", "800%", "1600%", "2400%", "3200%", "6400%"};    static final ImageIcon BFO16 = new ImageIcon(PDFViewer.class.getResource("resources/BFO16.png"));    // Components    private JDesktopPane desktop;    private JComboBox cboZoom;    private JButton filePrintButton;    private JMenuItem mCascade;    private JMenuItem mTiff;    private JPanel toolbarPanel;    // Other bits    private final JFrame parent;    private int frameCounter = 0;    private PDFDocumentFrame activeFrame;    private JFileChooser chooser;    /**     * Creates new PDFViewer     */    PDFViewer(JFrame frame) {        parent = frame;        try {            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (Exception e) { }        setPreferredSize(new Dimension(500, 500));        JPanel toolbarPanel = new javax.swing.JPanel();        toolbarPanel.setBorder(BorderFactory.createEtchedBorder());        JToolBar fileToolbar = new javax.swing.JToolBar();        fileToolbar.setLayout(new FlowLayout(FlowLayout.LEFT, 3, 0));        fileToolbar.setBorder(BorderFactory.createEmptyBorder());        JButton fileOpenButton = new javax.swing.JButton("Open");        fileOpenButton.setToolTipText("Open File...");        filePrintButton = new javax.swing.JButton("Print");        filePrintButton.setToolTipText("Print...");        filePrintButton.setEnabled(false);        JMenuBar mbPdf = new JMenuBar();        JMenu mFile = new JMenu();        JMenu mWindow = new JMenu();        mCascade = new JMenuItem();        mCascade.setEnabled(false);        JMenu mTools = new JMenu();        mTiff = new JMenuItem();        mTiff.setEnabled(false);        setLayout(new BorderLayout());        toolbarPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));        fileOpenButton.setPreferredSize(new Dimension(62, 32));        fileOpenButton.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                loadPDF(null);            }        });        fileToolbar.add(fileOpenButton);        filePrintButton.setPreferredSize(new Dimension(62, 32));        filePrintButton.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                activeFrame.print();            }        });        fileToolbar.add(filePrintButton);        cboZoom = new javax.swing.JComboBox(percentage);        if (System.getProperty("os.name").toLowerCase().startsWith("mac os x")) {            cboZoom.setFont(cboZoom.getFont().deriveFont(Font.PLAIN, 10));        }        cboZoom.setEnabled(false);        cboZoom.setSelectedIndex(4);        cboZoom.setPreferredSize(new Dimension(68, 32));        cboZoom.setEditable(true);        cboZoom.setToolTipText("Zoom Tool");        cboZoom.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                final JComboBox cb = (JComboBox) evt.getSource();                if (cb.getSelectedItem()!=null) {                    activeFrame.setPercent((String)cb.getSelectedItem());                }            }        });        fileToolbar.add(cboZoom);        toolbarPanel.add(fileToolbar);        add(toolbarPanel, BorderLayout.NORTH);        // add desktop pane        desktop = new JDesktopPane();        desktop.setBackground(Color.gray);        add(desktop,BorderLayout.CENTER);        mbPdf.setDoubleBuffered(true);        mFile.setText("File");        JMenuItem mFileOpen = new JMenuItem();        mFileOpen.setText("Open");        mFileOpen.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                loadPDF(null);            }        });        mFile.add(mFileOpen);        JMenuItem mFileQuit = new JMenuItem();        mFileQuit.setText("Quit");        mFileQuit.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                System.exit(0);            }        });        mFile.add(mFileQuit);        mbPdf.add(mFile);        mTools.setText("Tools");        mTiff.setText("PDF -> TIFF");        mTiff.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                PDFViewport vp = activeFrame.getViewPort();                TiffDialog tiff = new TiffDialog(parent, vp, true);            }        });        mTools.add(mTiff);        mbPdf.add(mTools);        mWindow.setText("Window");        mCascade.setText("Cascade");        mCascade.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                cascadeFrames();            }        });        mWindow.add(mCascade);        JMenuItem mAbout = new JMenuItem();        mAbout.setText("About");        mAbout.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                JOptionPane.showInternalMessageDialog(desktop, "Big Faceless PDF Library Viewer\nVersion "+PDF.VERSION+"\n\njava -jar bfopdf.jar --help for more options", "About", JOptionPane.INFORMATION_MESSAGE);            }        });        mWindow.add(mAbout);        mbPdf.add(mWindow);        parent.setJMenuBar(mbPdf);        setVisible(true);        new Thread() {            public void run() {                chooser = new JFileChooser((File)null);                chooser.setFileFilter(new FileFilter() {                    public boolean accept(File f) {                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".pdf");                    }                    public String getDescription() {                        return "PDF files (*.pdf)";                    }                });            }        }.start();    }    /**     * Load a PDF into the viewer from a file.     * @param file the PDF file to load, or <code>null</code> to select it with a chooser     * @since 2.7.1     */    public void loadPDF(File file) {        loadPDF(file, null);    }    /**     * Load a PDF into the viewer from a file. If a password is specified it will be used     * to open the file (if it's null or incorrect, the user will be prompted).     * @param file the PDF file to load, or <code>null</code> to select it with a chooser     * @param password the password to use to open the file - may be <code>null</code>.     * @since 2.7.1     */    public void loadPDF(File file, final String password) {        if (file == null) {            int returnVal = chooser.showOpenDialog(parent);            if (returnVal == JFileChooser.APPROVE_OPTION) {                repaint();                file = chooser.getSelectedFile();            }        }        if (file!=null) {            final File ffile = file;            javax.swing.SwingUtilities.invokeLater(new Runnable() {                public void run() {                    try {                        PDFReader reader = null;                        try {                            reader = new PDFReader(ffile, password);                        } catch (PasswordException pwd) {                            PasswordDialog login = new PasswordDialog(parent,ffile);                            repaint();                            if (login.isValidPassword()) {                                reader = login.getReader();                            }                        }                        if (reader!=null) {                            addDocumentFrame(new PDF(reader), ffile.getName());                        }                    } catch (Exception e) {                        activeFrameChanged();                        ErrorInfo.displayThrowable(e, "Open Document Error", parent);                    }                }            });        }    }    /**     * Load a pre-loaded PDF into the viewer.     * @param pdf the PDF to load     * @param name the name of the PDF, to display in the title bar.     * @since 2.7.1     */    public void loadPDF(final PDF pdf, final String name) {        if (pdf!=null) {            javax.swing.SwingUtilities.invokeLater(new Runnable() {                public void run() {                    addDocumentFrame(pdf, name==null ? pdf.getInfo("Title") : name);                }            });        }    }    private void addDocumentFrame(PDF pdf, String name) {        PDFDocumentFrame frame = new PDFDocumentFrame();        frame.setFrameIcon(BFO16);        frame.setBounds(frameCounter*30, frameCounter*20, INITWIDTH, INITHEIGHT);        frameCounter = (frameCounter+1) % 10;        frame.addInternalFrameListener(frame. new FrameListener());        try {            // minimise other frames            JInternalFrame[] fs = desktop.getAllFrames();            for (int i=0;i<fs.length;i++) fs[i].setMaximum(false);            // add new frame            desktop.add(frame);            frame.show();            frame.setMaximum(true);        } catch (java.beans.PropertyVetoException e) { }        frame.setTitle(name);        frame.setDocument(pdf);    }    private void activeFrameChanged() {        activeFrame = (PDFDocumentFrame)desktop.getSelectedFrame();        mCascade.setEnabled(activeFrame != null);        mTiff.setEnabled(activeFrame != null);        if(activeFrame != null) {            cboZoom.setSelectedItem(activeFrame.getPercent());        }        cboZoom.setEnabled(activeFrame != null);        filePrintButton.setEnabled(activeFrame != null);    }    private void cascadeFrames() {        try {            JInternalFrame[] frames = desktop.getAllFrames();            JInternalFrame selectedFrame = desktop.getSelectedFrame();            int x = 0;            int y = 0;            for (int k=frames.length-1; k>=0; k--) {                frames[k].setMaximum(false);                frames[k].setIcon(false);                frames[k].setBounds(x, y, INITWIDTH, INITHEIGHT);

⌨️ 快捷键说明

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