1 31 package org.pdfbox; 32 33 import org.pdfbox.exceptions.InvalidPasswordException; 34 35 import org.pdfbox.pdfviewer.PageWrapper; 36 import org.pdfbox.pdfviewer.ReaderBottomPanel; 37 38 import org.pdfbox.pdmodel.PDDocument; 39 import org.pdfbox.pdmodel.PDPage; 40 41 import org.pdfbox.util.ExtensionFileFilter; 42 43 import javax.swing.JFileChooser ; 44 import javax.swing.JScrollPane ; 45 import javax.swing.JPanel ; 46 47 import java.io.File ; 48 import java.io.FileInputStream ; 49 import java.io.InputStream ; 50 import java.io.IOException ; 51 import java.util.List ; 52 53 60 public class PDFReader extends javax.swing.JFrame 61 { 62 private File currentDir=new File ("."); 63 64 67 public PDFReader() 68 { 69 initComponents(); 70 } 71 72 78 private void initComponents() 79 { 80 menuBar = new javax.swing.JMenuBar (); 81 fileMenu = new javax.swing.JMenu (); 82 openMenuItem = new javax.swing.JMenuItem (); 83 saveMenuItem = new javax.swing.JMenuItem (); 84 saveAsMenuItem = new javax.swing.JMenuItem (); 85 exitMenuItem = new javax.swing.JMenuItem (); 86 editMenu = new javax.swing.JMenu (); 87 cutMenuItem = new javax.swing.JMenuItem (); 88 copyMenuItem = new javax.swing.JMenuItem (); 89 pasteMenuItem = new javax.swing.JMenuItem (); 90 deleteMenuItem = new javax.swing.JMenuItem (); 91 helpMenu = new javax.swing.JMenu (); 92 contentsMenuItem = new javax.swing.JMenuItem (); 93 aboutMenuItem = new javax.swing.JMenuItem (); 94 95 96 setTitle("PDFBox - PDF Reader"); 97 addWindowListener(new java.awt.event.WindowAdapter () 98 { 99 public void windowClosing(java.awt.event.WindowEvent evt) 100 { 101 exitForm(evt); 102 } 103 }); 104 105 106 JScrollPane documentScroller = new JScrollPane (); 107 documentScroller.setViewportView( documentPanel ); 108 109 110 getContentPane().add( documentScroller, java.awt.BorderLayout.CENTER ); 111 getContentPane().add( bottomStatusPanel, java.awt.BorderLayout.SOUTH ); 112 113 fileMenu.setText("File"); 114 openMenuItem.setText("Open"); 115 openMenuItem.setToolTipText("Open PDF file"); 116 openMenuItem.addActionListener(new java.awt.event.ActionListener () 117 { 118 public void actionPerformed(java.awt.event.ActionEvent evt) 119 { 120 openMenuItemActionPerformed(evt); 121 } 122 }); 123 124 fileMenu.add(openMenuItem); 125 126 saveMenuItem.setText("Save"); 127 129 saveAsMenuItem.setText("Save As ..."); 130 132 exitMenuItem.setText("Exit"); 133 exitMenuItem.addActionListener(new java.awt.event.ActionListener () 134 { 135 public void actionPerformed(java.awt.event.ActionEvent evt) 136 { 137 exitMenuItemActionPerformed(evt); 138 } 139 }); 140 141 fileMenu.add(exitMenuItem); 142 143 menuBar.add(fileMenu); 144 145 editMenu.setText("Edit"); 146 cutMenuItem.setText("Cut"); 147 editMenu.add(cutMenuItem); 148 149 copyMenuItem.setText("Copy"); 150 editMenu.add(copyMenuItem); 151 152 pasteMenuItem.setText("Paste"); 153 editMenu.add(pasteMenuItem); 154 155 deleteMenuItem.setText("Delete"); 156 editMenu.add(deleteMenuItem); 157 158 160 helpMenu.setText("Help"); 161 contentsMenuItem.setText("Contents"); 162 helpMenu.add(contentsMenuItem); 163 164 aboutMenuItem.setText("About"); 165 helpMenu.add(aboutMenuItem); 166 167 169 setJMenuBar(menuBar); 170 171 172 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 173 setBounds((screenSize.width-700)/2, (screenSize.height-600)/2, 700, 600); 174 } 176 private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) 177 { 178 JFileChooser chooser = new JFileChooser (); 179 chooser.setCurrentDirectory(currentDir); 180 181 ExtensionFileFilter pdfFilter = new ExtensionFileFilter(new String [] {"PDF"}, "PDF Files"); 182 chooser.setFileFilter(pdfFilter); 183 int result = chooser.showOpenDialog(PDFReader.this); 184 if (result == JFileChooser.APPROVE_OPTION) 185 { 186 String name = chooser.getSelectedFile().getPath(); 187 currentDir = new File (name).getParentFile(); 188 try 189 { 190 openPDFFile(name); 191 } 192 catch (Exception e) 193 { 194 e.printStackTrace(); 195 } 196 } 197 } 199 private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) 200 { 201 System.exit(0); 202 } 203 204 207 private void exitForm(java.awt.event.WindowEvent evt) 208 { 209 System.exit(0); 210 } 211 212 217 public static void main(String [] args) throws Exception 218 { 219 PDFReader viewer = new PDFReader(); 220 if( args.length >0 ) 221 { 222 viewer.openPDFFile( args[0] ); 223 } 224 viewer.show(); 225 } 226 227 private void openPDFFile(String file) throws Exception 228 { 229 if( document != null ) 230 { 231 document.close(); 232 documentPanel.removeAll(); 233 } 234 InputStream input = null; 235 File f = new File ( file ); 236 input = new FileInputStream (f); 237 document = parseDocument( input ); 238 setTitle( "PDFBox - " + f.getAbsolutePath() ); 239 240 List pages = document.getDocumentCatalog().getAllPages(); 241 for( int i=0; i<pages.size(); i++ ) 242 { 243 PageWrapper wrapper = new PageWrapper( this ); 244 wrapper.displayPage( (PDPage)pages.get(i) ); 245 documentPanel.add( wrapper.getPanel() ); 246 } 247 } 248 257 private static PDDocument parseDocument( InputStream input )throws IOException 258 { 259 PDDocument document = PDDocument.load( input ); 260 if( document.isEncrypted() ) 261 { 262 try 263 { 264 document.decrypt( "" ); 265 } 266 catch( InvalidPasswordException e ) 267 { 268 System.err.println( "Error: The document is encrypted." ); 269 } 270 catch( org.pdfbox.exceptions.CryptographyException e ) 271 { 272 e.printStackTrace(); 273 } 274 } 275 276 return document; 277 } 278 private javax.swing.JMenuItem aboutMenuItem; 280 private javax.swing.JMenuItem contentsMenuItem; 281 private javax.swing.JMenuItem copyMenuItem; 282 private javax.swing.JMenuItem cutMenuItem; 283 private javax.swing.JMenuItem deleteMenuItem; 284 private javax.swing.JMenu editMenu; 285 private javax.swing.JMenuItem exitMenuItem; 286 private javax.swing.JMenu fileMenu; 287 private javax.swing.JMenu helpMenu; 288 private javax.swing.JMenuBar menuBar; 289 private javax.swing.JMenuItem openMenuItem; 290 private javax.swing.JMenuItem pasteMenuItem; 291 private javax.swing.JMenuItem saveAsMenuItem; 292 private javax.swing.JMenuItem saveMenuItem; 293 private JPanel documentPanel = new JPanel (); 294 private ReaderBottomPanel bottomStatusPanel = new ReaderBottomPanel(); 295 297 private PDDocument document = null; 298 299 304 public ReaderBottomPanel getBottomStatusPanel() 305 { 306 return bottomStatusPanel; 307 } 308 } | Popular Tags |