pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1515127 [1/3] - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: PDFReader.java PrintPDF.java pdmodel/PDDocument.java pdmodel/PDPage.java pdmodel/PDPageable.java util/PDFImageWriter.java util/RenderUtil.java
Date Sun, 18 Aug 2013 15:02:08 GMT
Author: lehmi
Date: Sun Aug 18 15:02:07 2013
New Revision: 1515127

URL: http://svn.apache.org/r1515127
Log:
PDFBOX-1698: (re)move the print and rendering stuff

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/RenderUtil.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFReader.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PrintPDF.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFImageWriter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFReader.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFReader.java?rev=1515127&r1=1515126&r2=1515127&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFReader.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFReader.java Sun Aug 18 15:02:07
2013
@@ -16,6 +16,18 @@
  */
 package org.apache.pdfbox;
 
+import java.awt.image.BufferedImage;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+
+import javax.swing.JFileChooser;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.KeyStroke;
+
 import org.apache.pdfbox.exceptions.InvalidPasswordException;
 import org.apache.pdfbox.pdfviewer.PageWrapper;
 import org.apache.pdfbox.pdfviewer.ReaderBottomPanel;
@@ -24,29 +36,17 @@ import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDPageable;
 import org.apache.pdfbox.util.ExtensionFileFilter;
 import org.apache.pdfbox.util.ImageIOUtil;
-
-import javax.swing.JFileChooser;
-import javax.swing.JScrollPane;
-import javax.swing.JPanel;
-import javax.swing.KeyStroke;
-
-import java.awt.image.BufferedImage;
-import java.awt.print.PrinterException;
-import java.awt.print.PrinterJob;
-import java.io.File;
-import java.io.IOException;
-import java.util.List;
+import org.apache.pdfbox.util.RenderUtil;
 
 /**
- * An application to read PDF documents.  This will provide Acrobat Reader like
- * funtionality.
- *
+ * An application to read PDF documents. This will provide Acrobat Reader like funtionality.
+ * 
  * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
- * @version $Revision: 1.5 $
+ * 
  */
 public class PDFReader extends javax.swing.JFrame
 {
-    private File currentDir=new File(".");
+    private File currentDir = new File(".");
     private javax.swing.JMenuItem saveAsImageMenuItem;
     private javax.swing.JMenuItem exitMenuItem;
     private javax.swing.JMenu fileMenu;
@@ -60,16 +60,16 @@ public class PDFReader extends javax.swi
     private ReaderBottomPanel bottomStatusPanel = new ReaderBottomPanel();
 
     private PDDocument document = null;
-    private List<PDPage> pages= null;
-    
+    private List<PDPage> pages = null;
+
     private int currentPage = 0;
     private int numberOfPages = 0;
     private String currentFilename = null;
 
     private static final String PASSWORD = "-password";
     private static final String NONSEQ = "-nonSeq";
-    private static boolean useNonSeqParser = false; 
-    
+    private static boolean useNonSeqParser = false;
+
     /**
      * Constructor.
      */
@@ -79,10 +79,8 @@ public class PDFReader extends javax.swi
     }
 
     /**
-     * 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.
+     * This method is called from within the consructor to initialize the form.
+     * 
      */
     private void initComponents()
     {
@@ -96,7 +94,6 @@ public class PDFReader extends javax.swi
         nextPageItem = new javax.swing.JMenuItem();
         previousPageItem = new javax.swing.JMenuItem();
 
-
         setTitle("PDFBox - PDF Reader");
         addWindowListener(new java.awt.event.WindowAdapter()
         {
@@ -106,13 +103,11 @@ public class PDFReader extends javax.swi
             }
         });
 
-
         JScrollPane documentScroller = new JScrollPane();
-        documentScroller.setViewportView( documentPanel );
+        documentScroller.setViewportView(documentPanel);
 
-
-        getContentPane().add( documentScroller, java.awt.BorderLayout.CENTER );
-        getContentPane().add( bottomStatusPanel, java.awt.BorderLayout.SOUTH );
+        getContentPane().add(documentScroller, java.awt.BorderLayout.CENTER);
+        getContentPane().add(bottomStatusPanel, java.awt.BorderLayout.SOUTH);
 
         fileMenu.setText("File");
         openMenuItem.setText("Open");
@@ -127,14 +122,14 @@ public class PDFReader extends javax.swi
 
         fileMenu.add(openMenuItem);
 
-        printMenuItem.setText( "Print" );
+        printMenuItem.setText("Print");
         printMenuItem.addActionListener(new java.awt.event.ActionListener()
         {
             public void actionPerformed(java.awt.event.ActionEvent evt)
             {
                 try
                 {
-                    if (document != null) 
+                    if (document != null)
                     {
                         PDPageable pageable = new PDPageable(document);
                         PrinterJob job = pageable.getPrinterJob();
@@ -145,26 +140,26 @@ public class PDFReader extends javax.swi
                         }
                     }
                 }
-                catch( PrinterException e )
+                catch (PrinterException e)
                 {
                     e.printStackTrace();
                 }
             }
         });
-        fileMenu.add( printMenuItem );
+        fileMenu.add(printMenuItem);
 
-        saveAsImageMenuItem.setText( "Save as image" );
+        saveAsImageMenuItem.setText("Save as image");
         saveAsImageMenuItem.addActionListener(new java.awt.event.ActionListener()
         {
             public void actionPerformed(java.awt.event.ActionEvent evt)
             {
-                if (document != null) 
+                if (document != null)
                 {
                     saveImage();
                 }
             }
         });
-        fileMenu.add( saveAsImageMenuItem );
+        fileMenu.add(saveAsImageMenuItem);
 
         exitMenuItem.setText("Exit");
         exitMenuItem.addActionListener(new java.awt.event.ActionListener()
@@ -206,30 +201,28 @@ public class PDFReader extends javax.swi
 
         setJMenuBar(menuBar);
 
-
         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
-        setBounds((screenSize.width-700)/2, (screenSize.height-600)/2, 700, 600);
+        setBounds((screenSize.width - 700) / 2, (screenSize.height - 600) / 2, 700, 600);
     }
 
-
-    private void updateTitle() 
+    private void updateTitle()
     {
-        setTitle( "PDFBox - " + currentFilename + " ("+(currentPage+1)+"/"+numberOfPages+")");
+        setTitle("PDFBox - " + currentFilename + " (" + (currentPage + 1) + "/" + numberOfPages
+ ")");
     }
-    
+
     private void nextPage()
     {
-        if (currentPage < numberOfPages-1) 
+        if (currentPage < numberOfPages - 1)
         {
             currentPage++;
             updateTitle();
             showPage(currentPage);
         }
     }
-    
+
     private void previousPage()
     {
-        if (currentPage > 0 ) 
+        if (currentPage > 0)
         {
             currentPage--;
             updateTitle();
@@ -242,7 +235,7 @@ public class PDFReader extends javax.swi
         JFileChooser chooser = new JFileChooser();
         chooser.setCurrentDirectory(currentDir);
 
-        ExtensionFileFilter pdfFilter = new ExtensionFileFilter(new String[] {"PDF"}, "PDF
Files");
+        ExtensionFileFilter pdfFilter = new ExtensionFileFilter(new String[] { "PDF" }, "PDF
Files");
         chooser.setFileFilter(pdfFilter);
         int result = chooser.showOpenDialog(PDFReader.this);
         if (result == JFileChooser.APPROVE_OPTION)
@@ -264,22 +257,22 @@ public class PDFReader extends javax.swi
     {
         try
         {
-            if( document != null )
+            if (document != null)
             {
                 document.close();
             }
         }
-        catch( IOException io )
+        catch (IOException io)
         {
-            //do nothing because we are closing the application
+            // do nothing because we are closing the application
         }
-        this.setVisible( false );
+        this.setVisible(false);
         this.dispose();
     }
 
     /**
      * @param args the command line arguments
-     *
+     * 
      * @throws Exception If anything goes wrong.
      */
     public static void main(String[] args) throws Exception
@@ -287,18 +280,18 @@ public class PDFReader extends javax.swi
         PDFReader viewer = new PDFReader();
         String password = "";
         String filename = null;
-        for( int i = 0; i < args.length; i++ )
+        for (int i = 0; i < args.length; i++)
         {
-            if( args[i].equals( PASSWORD ) )
+            if (args[i].equals(PASSWORD))
             {
                 i++;
-                if( i >= args.length )
+                if (i >= args.length)
                 {
                     usage();
                 }
                 password = args[i];
             }
-            if( args[i].equals( NONSEQ ) )
+            if (args[i].equals(NONSEQ))
             {
                 useNonSeqParser = true;
             }
@@ -310,21 +303,21 @@ public class PDFReader extends javax.swi
         // open the pdf if present
         if (filename != null)
         {
-            viewer.openPDFFile( filename, password );
+            viewer.openPDFFile(filename, password);
         }
         viewer.setVisible(true);
     }
 
     private void openPDFFile(String filename, String password) throws Exception
     {
-        if( document != null )
+        if (document != null)
         {
             document.close();
             documentPanel.removeAll();
         }
-        
-        File file = new File( filename );
-        parseDocument( file, password );
+
+        File file = new File(filename);
+        parseDocument(file, password);
         pages = document.getDocumentCatalog().getAllPages();
         numberOfPages = pages.size();
         currentFilename = file.getAbsolutePath();
@@ -332,18 +325,18 @@ public class PDFReader extends javax.swi
         updateTitle();
         showPage(0);
     }
-    
+
     private void showPage(int pageNumber)
     {
-        try 
+        try
         {
-            PageWrapper wrapper = new PageWrapper( this );
-            wrapper.displayPage( (PDPage)pages.get(pageNumber) );
+            PageWrapper wrapper = new PageWrapper(this);
+            wrapper.displayPage((PDPage) pages.get(pageNumber));
             if (documentPanel.getComponentCount() > 0)
             {
                 documentPanel.remove(0);
             }
-            documentPanel.add( wrapper.getPanel() );
+            documentPanel.add(wrapper.getPanel());
             pack();
         }
         catch (IOException exception)
@@ -354,31 +347,31 @@ public class PDFReader extends javax.swi
 
     private void saveImage()
     {
-        try 
+        try
         {
-            PDPage pageToSave = (PDPage)pages.get(currentPage);
-            BufferedImage pageAsImage = pageToSave.convertToImage();
+            BufferedImage pageAsImage = RenderUtil.convertToImage(pages.get(currentPage));
             String imageFilename = currentFilename;
             if (imageFilename.toLowerCase().endsWith(".pdf"))
             {
-                imageFilename = imageFilename.substring(0, imageFilename.length()-4);
+                imageFilename = imageFilename.substring(0, imageFilename.length() - 4);
             }
-            imageFilename += "_" + (currentPage + 1); 
-            ImageIOUtil.writeImage(pageAsImage, "png", imageFilename,  BufferedImage.TYPE_USHORT_565_RGB,
300);
+            imageFilename += "_" + (currentPage + 1);
+            ImageIOUtil.writeImage(pageAsImage, "png", imageFilename, BufferedImage.TYPE_USHORT_565_RGB,
300);
         }
         catch (IOException exception)
         {
             exception.printStackTrace();
         }
     }
+
     /**
      * This will parse a document.
-     *
+     * 
      * @param input The input stream for the document.
-     *
+     * 
      * @throws IOException If there is an error parsing the document.
      */
-    private void parseDocument( File file, String password )throws IOException
+    private void parseDocument(File file, String password) throws IOException
     {
         document = null;
         if (useNonSeqParser)
@@ -388,17 +381,17 @@ public class PDFReader extends javax.swi
         else
         {
             document = PDDocument.load(file);
-            if( document.isEncrypted() )
+            if (document.isEncrypted())
             {
                 try
                 {
-                    document.decrypt( password );
+                    document.decrypt(password);
                 }
-                catch( InvalidPasswordException e )
+                catch (InvalidPasswordException e)
                 {
-                    System.err.println( "Error: The document is encrypted." );
+                    System.err.println("Error: The document is encrypted.");
                 }
-                catch( org.apache.pdfbox.exceptions.CryptographyException e )
+                catch (org.apache.pdfbox.exceptions.CryptographyException e)
                 {
                     e.printStackTrace();
                 }
@@ -408,25 +401,23 @@ public class PDFReader extends javax.swi
 
     /**
      * Get the bottom status panel.
-     *
+     * 
      * @return The bottom status panel.
      */
     public ReaderBottomPanel getBottomStatusPanel()
     {
         return bottomStatusPanel;
     }
-    
+
     /**
      * This will print out a message telling how to use this utility.
      */
     private static void usage()
     {
-        System.err.println(
-                "usage: java -jar pdfbox-app-x.y.z.jar PDFReader [OPTIONS] <input-file>\n"
+
-                "  -password <password>      Password to decrypt the document\n" +
-                "  -nonSeq                   Enables the new non-sequential parser\n" +
-                "  <input-file>              The PDF document to be loaded\n"
-                );
+        System.err.println("usage: java -jar pdfbox-app-x.y.z.jar PDFReader [OPTIONS] <input-file>\n"
+                + "  -password <password>      Password to decrypt the document\n"
+                + "  -nonSeq                   Enables the new non-sequential parser\n"
+                + "  <input-file>              The PDF document to be loaded\n");
     }
 
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PrintPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PrintPDF.java?rev=1515127&r1=1515126&r2=1515127&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PrintPDF.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PrintPDF.java Sun Aug 18 15:02:07
2013
@@ -17,69 +17,68 @@
 package org.apache.pdfbox;
 
 import java.awt.print.PrinterJob;
+import java.io.File;
 
 import javax.print.PrintService;
 
 import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdmodel.PDPageable;
-
-import java.io.File;
+import org.apache.pdfbox.util.RenderUtil;
 
 /**
  * This is a command line program that will print a PDF document.
- *
+ * 
  * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision: 1.4 $
  */
 public class PrintPDF
 {
 
-    private static final String PASSWORD     = "-password";
-    private static final String SILENT       = "-silentPrint";
+    private static final String PASSWORD = "-password";
+    private static final String SILENT = "-silentPrint";
     private static final String PRINTER_NAME = "-printerName";
 
     /**
      * private constructor.
-    */
+     */
     private PrintPDF()
     {
-        //static class
+        // static class
     }
 
     /**
      * Infamous main method.
-     *
+     * 
      * @param args Command line arguments, should be one and a reference to a file.
-     *
+     * 
      * @throws Exception If there is an error parsing the document.
      */
-    public static void main( String[] args ) throws Exception
+    public static void main(String[] args) throws Exception
     {
         String password = "";
         String pdfFile = null;
         boolean silentPrint = false;
         String printerName = null;
-        for( int i=0; i<args.length; i++ )
+        for (int i = 0; i < args.length; i++)
         {
-            if( args[i].equals( PASSWORD ) )
+            if (args[i].equals(PASSWORD))
             {
                 i++;
-                if( i >= args.length )
+                if (i >= args.length)
                 {
                     usage();
                 }
                 password = args[i];
             }
-            else if( args[i].equals( PRINTER_NAME ) )
+            else if (args[i].equals(PRINTER_NAME))
             {
                 i++;
-                if( i >= args.length )
+                if (i >= args.length)
                 {
                     usage();
                 }
                 printerName = args[i];
             }
-            else if( args[i].equals( SILENT ) )
+            else if (args[i].equals(SILENT))
             {
                 silentPrint = true;
             }
@@ -89,7 +88,7 @@ public class PrintPDF
             }
         }
 
-        if( pdfFile == null )
+        if (pdfFile == null)
         {
             usage();
         }
@@ -97,23 +96,23 @@ public class PrintPDF
         PDDocument document = null;
         try
         {
-            document = PDDocument.load( pdfFile );
+            document = PDDocument.load(pdfFile);
 
-            if( document.isEncrypted() )
+            if (document.isEncrypted())
             {
-                document.decrypt( password );
+                document.decrypt(password);
             }
 
             PrinterJob printJob = PrinterJob.getPrinterJob();
             printJob.setJobName(new File(pdfFile).getName());
 
-            if(printerName != null )
+            if (printerName != null)
             {
                 PrintService[] printService = PrinterJob.lookupPrintServices();
                 boolean printerFound = false;
-                for(int i = 0; !printerFound && i < printService.length; i++)
+                for (int i = 0; !printerFound && i < printService.length; i++)
                 {
-                    if(printService[i].getName().indexOf(printerName) != -1)
+                    if (printService[i].getName().indexOf(printerName) != -1)
                     {
                         printJob.setPrintService(printService[i]);
                         printerFound = true;
@@ -121,15 +120,18 @@ public class PrintPDF
                 }
             }
 
-            printJob.setPageable(new PDPageable(document, printJob));
-            if( silentPrint || printJob.printDialog())
+            if (silentPrint)
+            {
+                RenderUtil.silentPrint(document, printJob);
+            }
+            else if (printJob.printDialog())
             {
-                printJob.print();
+                RenderUtil.print(document, printJob);
             }
         }
         finally
         {
-            if( document != null )
+            if (document != null)
             {
                 document.close();
             }
@@ -141,10 +143,9 @@ public class PrintPDF
      */
     private static void usage()
     {
-        System.err.println( "Usage: java -jar pdfbox-app-x.y.z.jar PrintPDF [OPTIONS] <PDF
file>\n" +
-            "  -password  <password>        Password to decrypt document\n" +
-            "  -silentPrint                 Print without prompting for printer info\n"
-            );
-        System.exit( 1 );
+        System.err.println("Usage: java -jar pdfbox-app-x.y.z.jar PrintPDF [OPTIONS] <PDF
file>\n"
+                + "  -password  <password>        Password to decrypt document\n"
+                + "  -silentPrint                 Print without prompting for printer info\n");
+        System.exit(1);
     }
 }



Mime
View raw message