001: /**
002: * Copyright (c) 2005-2006, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox;
031:
032: import org.pdfbox.exceptions.InvalidPasswordException;
033:
034: import org.pdfbox.pdfviewer.PageWrapper;
035: import org.pdfbox.pdfviewer.ReaderBottomPanel;
036:
037: import org.pdfbox.pdmodel.PDDocument;
038: import org.pdfbox.pdmodel.PDPage;
039:
040: import org.pdfbox.util.ExtensionFileFilter;
041:
042: import javax.swing.JFileChooser;
043: import javax.swing.JScrollPane;
044: import javax.swing.JPanel;
045:
046: import java.io.File;
047: import java.io.FileInputStream;
048: import java.io.InputStream;
049: import java.io.IOException;
050: import java.util.List;
051:
052: /**
053: * An application to read PDF documents. This will provide Acrobat Reader like
054: * funtionality.
055: *
056: * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
057: * @version $Revision: 1.4 $
058: */
059: public class PDFReader extends javax.swing.JFrame {
060: private File currentDir = new File(".");
061:
062: /**
063: * Constructor.
064: */
065: public PDFReader() {
066: initComponents();
067: }
068:
069: /**
070: * This method is called from within the constructor to
071: * initialize the form.
072: * WARNING: Do NOT modify this code. The content of this method is
073: * always regenerated by the Form Editor.
074: */
075: private void initComponents() {
076: menuBar = new javax.swing.JMenuBar();
077: fileMenu = new javax.swing.JMenu();
078: openMenuItem = new javax.swing.JMenuItem();
079: saveMenuItem = new javax.swing.JMenuItem();
080: saveAsMenuItem = new javax.swing.JMenuItem();
081: exitMenuItem = new javax.swing.JMenuItem();
082: editMenu = new javax.swing.JMenu();
083: cutMenuItem = new javax.swing.JMenuItem();
084: copyMenuItem = new javax.swing.JMenuItem();
085: pasteMenuItem = new javax.swing.JMenuItem();
086: deleteMenuItem = new javax.swing.JMenuItem();
087: helpMenu = new javax.swing.JMenu();
088: contentsMenuItem = new javax.swing.JMenuItem();
089: aboutMenuItem = new javax.swing.JMenuItem();
090:
091: setTitle("PDFBox - PDF Reader");
092: addWindowListener(new java.awt.event.WindowAdapter() {
093: public void windowClosing(java.awt.event.WindowEvent evt) {
094: exitForm(evt);
095: }
096: });
097:
098: JScrollPane documentScroller = new JScrollPane();
099: documentScroller.setViewportView(documentPanel);
100:
101: getContentPane().add(documentScroller,
102: java.awt.BorderLayout.CENTER);
103: getContentPane().add(bottomStatusPanel,
104: java.awt.BorderLayout.SOUTH);
105:
106: fileMenu.setText("File");
107: openMenuItem.setText("Open");
108: openMenuItem.setToolTipText("Open PDF file");
109: openMenuItem
110: .addActionListener(new java.awt.event.ActionListener() {
111: public void actionPerformed(
112: java.awt.event.ActionEvent evt) {
113: openMenuItemActionPerformed(evt);
114: }
115: });
116:
117: fileMenu.add(openMenuItem);
118:
119: saveMenuItem.setText("Save");
120: //fileMenu.add(saveMenuItem);
121:
122: saveAsMenuItem.setText("Save As ...");
123: //fileMenu.add(saveAsMenuItem);
124:
125: exitMenuItem.setText("Exit");
126: exitMenuItem
127: .addActionListener(new java.awt.event.ActionListener() {
128: public void actionPerformed(
129: java.awt.event.ActionEvent evt) {
130: exitMenuItemActionPerformed(evt);
131: }
132: });
133:
134: fileMenu.add(exitMenuItem);
135:
136: menuBar.add(fileMenu);
137:
138: editMenu.setText("Edit");
139: cutMenuItem.setText("Cut");
140: editMenu.add(cutMenuItem);
141:
142: copyMenuItem.setText("Copy");
143: editMenu.add(copyMenuItem);
144:
145: pasteMenuItem.setText("Paste");
146: editMenu.add(pasteMenuItem);
147:
148: deleteMenuItem.setText("Delete");
149: editMenu.add(deleteMenuItem);
150:
151: //menuBar.add(editMenu);
152:
153: helpMenu.setText("Help");
154: contentsMenuItem.setText("Contents");
155: helpMenu.add(contentsMenuItem);
156:
157: aboutMenuItem.setText("About");
158: helpMenu.add(aboutMenuItem);
159:
160: //menuBar.add(helpMenu);
161:
162: setJMenuBar(menuBar);
163:
164: java.awt.Dimension screenSize = java.awt.Toolkit
165: .getDefaultToolkit().getScreenSize();
166: setBounds((screenSize.width - 700) / 2,
167: (screenSize.height - 600) / 2, 700, 600);
168: }//GEN-END:initComponents
169:
170: private void openMenuItemActionPerformed(
171: java.awt.event.ActionEvent evt) {
172: JFileChooser chooser = new JFileChooser();
173: chooser.setCurrentDirectory(currentDir);
174:
175: ExtensionFileFilter pdfFilter = new ExtensionFileFilter(
176: new String[] { "PDF" }, "PDF Files");
177: chooser.setFileFilter(pdfFilter);
178: int result = chooser.showOpenDialog(PDFReader.this );
179: if (result == JFileChooser.APPROVE_OPTION) {
180: String name = chooser.getSelectedFile().getPath();
181: currentDir = new File(name).getParentFile();
182: try {
183: openPDFFile(name);
184: } catch (Exception e) {
185: e.printStackTrace();
186: }
187: }
188: }//GEN-LAST:event_openMenuItemActionPerformed
189:
190: private void exitMenuItemActionPerformed(
191: java.awt.event.ActionEvent evt) {
192: System.exit(0);
193: }
194:
195: /**
196: * Exit the Application.
197: */
198: private void exitForm(java.awt.event.WindowEvent evt) {
199: System.exit(0);
200: }
201:
202: /**
203: * @param args the command line arguments
204: *
205: * @throws Exception If anything goes wrong.
206: */
207: public static void main(String[] args) throws Exception {
208: PDFReader viewer = new PDFReader();
209: if (args.length > 0) {
210: viewer.openPDFFile(args[0]);
211: }
212: viewer.show();
213: }
214:
215: private void openPDFFile(String file) throws Exception {
216: if (document != null) {
217: document.close();
218: documentPanel.removeAll();
219: }
220: InputStream input = null;
221: File f = new File(file);
222: input = new FileInputStream(f);
223: document = parseDocument(input);
224: setTitle("PDFBox - " + f.getAbsolutePath());
225:
226: List pages = document.getDocumentCatalog().getAllPages();
227: for (int i = 0; i < pages.size(); i++) {
228: PageWrapper wrapper = new PageWrapper(this );
229: wrapper.displayPage((PDPage) pages.get(i));
230: documentPanel.add(wrapper.getPanel());
231: }
232: }
233:
234: /**
235: * This will parse a document.
236: *
237: * @param input The input stream for the document.
238: *
239: * @return The document.
240: *
241: * @throws IOException If there is an error parsing the document.
242: */
243: private static PDDocument parseDocument(InputStream input)
244: throws IOException {
245: PDDocument document = PDDocument.load(input);
246: if (document.isEncrypted()) {
247: try {
248: document.decrypt("");
249: } catch (InvalidPasswordException e) {
250: System.err.println("Error: The document is encrypted.");
251: } catch (org.pdfbox.exceptions.CryptographyException e) {
252: e.printStackTrace();
253: }
254: }
255:
256: return document;
257: }
258:
259: // Variables declaration - do not modify//GEN-BEGIN:variables
260: private javax.swing.JMenuItem aboutMenuItem;
261: private javax.swing.JMenuItem contentsMenuItem;
262: private javax.swing.JMenuItem copyMenuItem;
263: private javax.swing.JMenuItem cutMenuItem;
264: private javax.swing.JMenuItem deleteMenuItem;
265: private javax.swing.JMenu editMenu;
266: private javax.swing.JMenuItem exitMenuItem;
267: private javax.swing.JMenu fileMenu;
268: private javax.swing.JMenu helpMenu;
269: private javax.swing.JMenuBar menuBar;
270: private javax.swing.JMenuItem openMenuItem;
271: private javax.swing.JMenuItem pasteMenuItem;
272: private javax.swing.JMenuItem saveAsMenuItem;
273: private javax.swing.JMenuItem saveMenuItem;
274: private JPanel documentPanel = new JPanel();
275: private ReaderBottomPanel bottomStatusPanel = new ReaderBottomPanel();
276: // End of variables declaration//GEN-END:variables
277:
278: private PDDocument document = null;
279:
280: /**
281: * Get the bottom status panel.
282: *
283: * @return The bottom status panel.
284: */
285: public ReaderBottomPanel getBottomStatusPanel() {
286: return bottomStatusPanel;
287: }
288: }
|