aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/pdfbox/PDFReader.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/pdfbox/PDFReader.java')
-rw-r--r--src/main/java/org/pdfbox/PDFReader.java308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/main/java/org/pdfbox/PDFReader.java b/src/main/java/org/pdfbox/PDFReader.java
new file mode 100644
index 0000000..cf9f548
--- /dev/null
+++ b/src/main/java/org/pdfbox/PDFReader.java
@@ -0,0 +1,308 @@
+/**
+ * Copyright (c) 2005, www.pdfbox.org
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of pdfbox; nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * http://www.pdfbox.org
+ *
+ */
+package org.pdfbox;
+
+import org.pdfbox.exceptions.InvalidPasswordException;
+
+import org.pdfbox.pdfviewer.PageWrapper;
+import org.pdfbox.pdfviewer.ReaderBottomPanel;
+
+import org.pdfbox.pdmodel.PDDocument;
+import org.pdfbox.pdmodel.PDPage;
+
+import org.pdfbox.util.DefaultFileFilter;
+
+import javax.swing.JFileChooser;
+import javax.swing.JScrollPane;
+import javax.swing.JPanel;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * An application to read PDF documents. This will provide Acrobat Reader like
+ * funtionality.
+ *
+ * @author Ben Litchfield (ben@pdfbox.org)
+ * @version $Revision: 1.2 $
+ */
+public class PDFReader extends javax.swing.JFrame
+{
+ private File currentDir=new File(".");
+
+ /**
+ * Constructor.
+ */
+ public PDFReader()
+ {
+ initComponents();
+ }
+
+ /**
+ * This method is called from within the constructor to
+ * initialize the form.
+ * WARNING: Do NOT modify this code. The content of this method is
+ * always regenerated by the Form Editor.
+ */
+ private void initComponents()
+ {
+ menuBar = new javax.swing.JMenuBar();
+ fileMenu = new javax.swing.JMenu();
+ openMenuItem = new javax.swing.JMenuItem();
+ saveMenuItem = new javax.swing.JMenuItem();
+ saveAsMenuItem = new javax.swing.JMenuItem();
+ exitMenuItem = new javax.swing.JMenuItem();
+ editMenu = new javax.swing.JMenu();
+ cutMenuItem = new javax.swing.JMenuItem();
+ copyMenuItem = new javax.swing.JMenuItem();
+ pasteMenuItem = new javax.swing.JMenuItem();
+ deleteMenuItem = new javax.swing.JMenuItem();
+ helpMenu = new javax.swing.JMenu();
+ contentsMenuItem = new javax.swing.JMenuItem();
+ aboutMenuItem = new javax.swing.JMenuItem();
+
+
+ setTitle("PDFBox - PDF Reader");
+ addWindowListener(new java.awt.event.WindowAdapter()
+ {
+ public void windowClosing(java.awt.event.WindowEvent evt)
+ {
+ exitForm(evt);
+ }
+ });
+
+
+ JScrollPane documentScroller = new JScrollPane();
+ documentScroller.setViewportView( documentPanel );
+
+
+ getContentPane().add( documentScroller, java.awt.BorderLayout.CENTER );
+ getContentPane().add( bottomStatusPanel, java.awt.BorderLayout.SOUTH );
+
+ fileMenu.setText("File");
+ openMenuItem.setText("Open");
+ openMenuItem.setToolTipText("Open PDF file");
+ openMenuItem.addActionListener(new java.awt.event.ActionListener()
+ {
+ public void actionPerformed(java.awt.event.ActionEvent evt)
+ {
+ openMenuItemActionPerformed(evt);
+ }
+ });
+
+ fileMenu.add(openMenuItem);
+
+ saveMenuItem.setText("Save");
+ //fileMenu.add(saveMenuItem);
+
+ saveAsMenuItem.setText("Save As ...");
+ //fileMenu.add(saveAsMenuItem);
+
+ exitMenuItem.setText("Exit");
+ exitMenuItem.addActionListener(new java.awt.event.ActionListener()
+ {
+ public void actionPerformed(java.awt.event.ActionEvent evt)
+ {
+ exitMenuItemActionPerformed(evt);
+ }
+ });
+
+ fileMenu.add(exitMenuItem);
+
+ menuBar.add(fileMenu);
+
+ editMenu.setText("Edit");
+ cutMenuItem.setText("Cut");
+ editMenu.add(cutMenuItem);
+
+ copyMenuItem.setText("Copy");
+ editMenu.add(copyMenuItem);
+
+ pasteMenuItem.setText("Paste");
+ editMenu.add(pasteMenuItem);
+
+ deleteMenuItem.setText("Delete");
+ editMenu.add(deleteMenuItem);
+
+ //menuBar.add(editMenu);
+
+ helpMenu.setText("Help");
+ contentsMenuItem.setText("Contents");
+ helpMenu.add(contentsMenuItem);
+
+ aboutMenuItem.setText("About");
+ helpMenu.add(aboutMenuItem);
+
+ //menuBar.add(helpMenu);
+
+ setJMenuBar(menuBar);
+
+
+ java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
+ setBounds((screenSize.width-700)/2, (screenSize.height-600)/2, 700, 600);
+ }//GEN-END:initComponents
+
+ private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt)
+ {
+ JFileChooser chooser = new JFileChooser();
+ chooser.setCurrentDirectory(currentDir);
+
+ DefaultFileFilter pdfFilter = new DefaultFileFilter(new String[] {"pdf", "PDF"}, "PDF Files");
+ chooser.setFileFilter(pdfFilter);
+ int result = chooser.showOpenDialog(PDFReader.this);
+ if (result == JFileChooser.APPROVE_OPTION)
+ {
+ String name = chooser.getSelectedFile().getPath();
+ currentDir = new File(name).getParentFile();
+ try
+ {
+ openPDFFile(name);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }//GEN-LAST:event_openMenuItemActionPerformed
+
+ private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt)
+ {
+ System.exit(0);
+ }
+
+ /**
+ * Exit the Application.
+ */
+ private void exitForm(java.awt.event.WindowEvent evt)
+ {
+ System.exit(0);
+ }
+
+ /**
+ * @param args the command line arguments
+ *
+ * @throws Exception If anything goes wrong.
+ */
+ public static void main(String[] args) throws Exception
+ {
+ PDFReader viewer = new PDFReader();
+ if( args.length >0 )
+ {
+ viewer.openPDFFile( args[0] );
+ }
+ viewer.show();
+ }
+
+ private void openPDFFile(String file) throws Exception
+ {
+ if( document != null )
+ {
+ document.close();
+ documentPanel.removeAll();
+ }
+ InputStream input = null;
+ File f = new File( file );
+ input = new FileInputStream(f);
+ document = parseDocument( input );
+ setTitle( "PDFBox - " + f.getAbsolutePath() );
+
+ List pages = document.getDocumentCatalog().getAllPages();
+ for( int i=0; i<pages.size(); i++ )
+ {
+ PageWrapper wrapper = new PageWrapper( this );
+ wrapper.displayPage( (PDPage)pages.get(i) );
+ documentPanel.add( wrapper.getPanel() );
+ }
+ }
+ /**
+ * This will parse a document.
+ *
+ * @param input The input stream for the document.
+ *
+ * @return The document.
+ *
+ * @throws IOException If there is an error parsing the document.
+ */
+ private static PDDocument parseDocument( InputStream input )throws IOException
+ {
+ PDDocument document = PDDocument.load( input );
+ if( document.isEncrypted() )
+ {
+ try
+ {
+ document.decrypt( "" );
+ }
+ catch( InvalidPasswordException e )
+ {
+ System.err.println( "Error: The document is encrypted." );
+ }
+ catch( org.pdfbox.exceptions.CryptographyException e )
+ {
+ e.printStackTrace();
+ }
+ }
+
+ return document;
+ }
+ // Variables declaration - do not modify//GEN-BEGIN:variables
+ private javax.swing.JMenuItem aboutMenuItem;
+ private javax.swing.JMenuItem contentsMenuItem;
+ private javax.swing.JMenuItem copyMenuItem;
+ private javax.swing.JMenuItem cutMenuItem;
+ private javax.swing.JMenuItem deleteMenuItem;
+ private javax.swing.JMenu editMenu;
+ private javax.swing.JMenuItem exitMenuItem;
+ private javax.swing.JMenu fileMenu;
+ private javax.swing.JMenu helpMenu;
+ private javax.swing.JMenuBar menuBar;
+ private javax.swing.JMenuItem openMenuItem;
+ private javax.swing.JMenuItem pasteMenuItem;
+ private javax.swing.JMenuItem saveAsMenuItem;
+ private javax.swing.JMenuItem saveMenuItem;
+ private JPanel documentPanel = new JPanel();
+ private ReaderBottomPanel bottomStatusPanel = new ReaderBottomPanel();
+ // End of variables declaration//GEN-END:variables
+
+ private PDDocument document = null;
+
+ /**
+ * Get the bottom status panel.
+ *
+ * @return The bottom status panel.
+ */
+ public ReaderBottomPanel getBottomStatusPanel()
+ {
+ return bottomStatusPanel;
+ }
+} \ No newline at end of file