KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > PDFReader


1 /**
2  * Copyright (c) 2005-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

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 JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.util.List JavaDoc;
52
53 /**
54  * An application to read PDF documents. This will provide Acrobat Reader like
55  * funtionality.
56  *
57  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
58  * @version $Revision: 1.4 $
59  */

60 public class PDFReader extends javax.swing.JFrame JavaDoc
61 {
62     private File JavaDoc currentDir=new File JavaDoc(".");
63
64     /**
65      * Constructor.
66      */

67     public PDFReader()
68     {
69         initComponents();
70     }
71
72     /**
73      * This method is called from within the constructor to
74      * initialize the form.
75      * WARNING: Do NOT modify this code. The content of this method is
76      * always regenerated by the Form Editor.
77      */

78     private void initComponents()
79     {
80         menuBar = new javax.swing.JMenuBar JavaDoc();
81         fileMenu = new javax.swing.JMenu JavaDoc();
82         openMenuItem = new javax.swing.JMenuItem JavaDoc();
83         saveMenuItem = new javax.swing.JMenuItem JavaDoc();
84         saveAsMenuItem = new javax.swing.JMenuItem JavaDoc();
85         exitMenuItem = new javax.swing.JMenuItem JavaDoc();
86         editMenu = new javax.swing.JMenu JavaDoc();
87         cutMenuItem = new javax.swing.JMenuItem JavaDoc();
88         copyMenuItem = new javax.swing.JMenuItem JavaDoc();
89         pasteMenuItem = new javax.swing.JMenuItem JavaDoc();
90         deleteMenuItem = new javax.swing.JMenuItem JavaDoc();
91         helpMenu = new javax.swing.JMenu JavaDoc();
92         contentsMenuItem = new javax.swing.JMenuItem JavaDoc();
93         aboutMenuItem = new javax.swing.JMenuItem JavaDoc();
94
95
96         setTitle("PDFBox - PDF Reader");
97         addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
98         {
99             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
100             {
101                 exitForm(evt);
102             }
103         });
104
105
106         JScrollPane JavaDoc documentScroller = new JScrollPane JavaDoc();
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 JavaDoc()
117         {
118             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
119             {
120                 openMenuItemActionPerformed(evt);
121             }
122         });
123
124         fileMenu.add(openMenuItem);
125
126         saveMenuItem.setText("Save");
127         //fileMenu.add(saveMenuItem);
128

129         saveAsMenuItem.setText("Save As ...");
130         //fileMenu.add(saveAsMenuItem);
131

132         exitMenuItem.setText("Exit");
133         exitMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc()
134         {
135             public void actionPerformed(java.awt.event.ActionEvent JavaDoc 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         //menuBar.add(editMenu);
159

160         helpMenu.setText("Help");
161         contentsMenuItem.setText("Contents");
162         helpMenu.add(contentsMenuItem);
163
164         aboutMenuItem.setText("About");
165         helpMenu.add(aboutMenuItem);
166
167         //menuBar.add(helpMenu);
168

169         setJMenuBar(menuBar);
170
171
172         java.awt.Dimension JavaDoc screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
173         setBounds((screenSize.width-700)/2, (screenSize.height-600)/2, 700, 600);
174     }//GEN-END:initComponents
175

176     private void openMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt)
177     {
178         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
179         chooser.setCurrentDirectory(currentDir);
180
181         ExtensionFileFilter pdfFilter = new ExtensionFileFilter(new String JavaDoc[] {"PDF"}, "PDF Files");
182         chooser.setFileFilter(pdfFilter);
183         int result = chooser.showOpenDialog(PDFReader.this);
184         if (result == JFileChooser.APPROVE_OPTION)
185         {
186             String JavaDoc name = chooser.getSelectedFile().getPath();
187             currentDir = new File JavaDoc(name).getParentFile();
188             try
189             {
190                 openPDFFile(name);
191             }
192             catch (Exception JavaDoc e)
193             {
194                 e.printStackTrace();
195             }
196         }
197     }//GEN-LAST:event_openMenuItemActionPerformed
198

199     private void exitMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt)
200     {
201         System.exit(0);
202     }
203
204     /**
205      * Exit the Application.
206      */

207     private void exitForm(java.awt.event.WindowEvent JavaDoc evt)
208     {
209         System.exit(0);
210     }
211
212     /**
213      * @param args the command line arguments
214      *
215      * @throws Exception If anything goes wrong.
216      */

217     public static void main(String JavaDoc[] args) throws Exception JavaDoc
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 JavaDoc file) throws Exception JavaDoc
228     {
229         if( document != null )
230         {
231             document.close();
232             documentPanel.removeAll();
233         }
234         InputStream JavaDoc input = null;
235         File JavaDoc f = new File JavaDoc( file );
236         input = new FileInputStream JavaDoc(f);
237         document = parseDocument( input );
238         setTitle( "PDFBox - " + f.getAbsolutePath() );
239         
240         List JavaDoc 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     /**
249      * This will parse a document.
250      *
251      * @param input The input stream for the document.
252      *
253      * @return The document.
254      *
255      * @throws IOException If there is an error parsing the document.
256      */

257     private static PDDocument parseDocument( InputStream JavaDoc input )throws IOException JavaDoc
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     // Variables declaration - do not modify//GEN-BEGIN:variables
279
private javax.swing.JMenuItem JavaDoc aboutMenuItem;
280     private javax.swing.JMenuItem JavaDoc contentsMenuItem;
281     private javax.swing.JMenuItem JavaDoc copyMenuItem;
282     private javax.swing.JMenuItem JavaDoc cutMenuItem;
283     private javax.swing.JMenuItem JavaDoc deleteMenuItem;
284     private javax.swing.JMenu JavaDoc editMenu;
285     private javax.swing.JMenuItem JavaDoc exitMenuItem;
286     private javax.swing.JMenu JavaDoc fileMenu;
287     private javax.swing.JMenu JavaDoc helpMenu;
288     private javax.swing.JMenuBar JavaDoc menuBar;
289     private javax.swing.JMenuItem JavaDoc openMenuItem;
290     private javax.swing.JMenuItem JavaDoc pasteMenuItem;
291     private javax.swing.JMenuItem JavaDoc saveAsMenuItem;
292     private javax.swing.JMenuItem JavaDoc saveMenuItem;
293     private JPanel JavaDoc documentPanel = new JPanel JavaDoc();
294     private ReaderBottomPanel bottomStatusPanel = new ReaderBottomPanel();
295     // End of variables declaration//GEN-END:variables
296

297     private PDDocument document = null;
298
299     /**
300      * Get the bottom status panel.
301      *
302      * @return The bottom status panel.
303      */

304     public ReaderBottomPanel getBottomStatusPanel()
305     {
306         return bottomStatusPanel;
307     }
308 }
Popular Tags