You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2012/02/04 18:34:45 UTC

svn commit: r1240556 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: PDFSplit.java util/Splitter.java

Author: lehmi
Date: Sat Feb  4 17:34:45 2012
New Revision: 1240556

URL: http://svn.apache.org/viewvc?rev=1240556&view=rev
Log:
PDFBOX-1221: added a startPage and endPage parameter to PDFSplit

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFSplit.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Splitter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFSplit.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFSplit.java?rev=1240556&r1=1240555&r2=1240556&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFSplit.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/PDFSplit.java Sat Feb  4 17:34:45 2012
@@ -45,6 +45,8 @@ public class PDFSplit
 {
     private static final String PASSWORD = "-password";
     private static final String SPLIT = "-split";
+    private static final String START_PAGE = "-startPage";
+    private static final String END_PAGE = "-endPage";
 
     private PDFSplit()
     {
@@ -65,8 +67,10 @@ public class PDFSplit
     private void split( String[] args ) throws Exception
     {
         String password = "";
-        String split = "1";
-
+        String split = null;
+        String startPage = null;
+        String endPage = null;
+        
         Splitter splitter = new Splitter();
         String pdfFile = null;
         for( int i=0; i<args.length; i++ )
@@ -89,6 +93,24 @@ public class PDFSplit
                 }
                 split = args[i];
             }
+            else if( args[i].equals( START_PAGE ) )
+            {
+                i++;
+                if( i >= args.length )
+                {
+                    usage();
+                }
+                startPage = args[i];
+            }
+            else if( args[i].equals( END_PAGE ) )
+            {
+                i++;
+                if( i >= args.length )
+                {
+                    usage();
+                }
+                endPage = args[i];
+            }
             else
             {
                 if( pdfFile == null )
@@ -107,7 +129,7 @@ public class PDFSplit
 
             InputStream input = null;
             PDDocument document = null;
-            List documents = null;
+            List<PDDocument> documents = null;
             try
             {
                 input = new FileInputStream( pdfFile );
@@ -128,18 +150,47 @@ public class PDFSplit
                         }
                         else
                         {
-                            //they didn't suppply a password and the default of "" was wrong.
+                            //they didn't supply a password and the default of "" was wrong.
                             System.err.println( "Error: The document is encrypted." );
                             usage();
                         }
                     }
                 }
 
-                splitter.setSplitAtPage( Integer.parseInt( split ) );
+                int numberOfPages = document.getNumberOfPages();
+                boolean startEndPageSet = false;
+                if (startPage != null)
+                {
+                    splitter.setStartPage(Integer.parseInt( startPage ));
+                    if (split == null)
+                    {
+                        splitter.setSplitAtPage(numberOfPages);
+                    }
+                }
+                if (endPage != null)
+                {
+                    splitter.setEndPage(Integer.parseInt( endPage ));
+                    if (split == null)
+                    {
+                        splitter.setSplitAtPage(Integer.parseInt( endPage ));
+                    }
+                }
+                if (split != null)
+                {
+                    splitter.setSplitAtPage( Integer.parseInt( split ) );
+                }
+                else 
+                {
+                    if (!startEndPageSet)
+                    {
+                        splitter.setSplitAtPage(1);
+                    }
+                }
+                    
                 documents = splitter.split( document );
                 for( int i=0; i<documents.size(); i++ )
                 {
-                    PDDocument doc = (PDDocument)documents.get( i );
+                    PDDocument doc = documents.get( i );
                     String fileName = pdfFile.substring(0, pdfFile.length()-4 ) + "-" + i + ".pdf";
                     writeDocument( doc, fileName );
                     doc.close();
@@ -210,9 +261,11 @@ public class PDFSplit
     private static void usage()
     {
         System.err.println( "Usage: java -jar pdfbox-app-x.y.z.jar PDFSplit [OPTIONS] <PDF file>\n" +
-            "  -password  <password>        Password to decrypt document\n" +
-            "  -split     <integer>         split after this many pages\n" +
-            "  <PDF file>                   The PDF document to use\n"
+            "  -password  <password>  Password to decrypt document\n" +
+            "  -split     <integer>   split after this many pages (default 1, if startPage and endPage are unset)\n"+
+            "  -startPage <integer>   start page\n" +
+            "  -endPage   <integer>   end page\n" +
+            "  <PDF file>             The PDF document to use\n"
             );
         System.exit( 1 );
     }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Splitter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Splitter.java?rev=1240556&r1=1240555&r2=1240556&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Splitter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Splitter.java Sat Feb  4 17:34:45 2012
@@ -45,7 +45,9 @@ public class Splitter
      */
     protected PDDocument currentDocument = null;
 
-    private int splitAtPage=1;
+    private int splitAtPage = 1;
+    private int startPage = Integer.MIN_VALUE;
+    private int endPage = Integer.MAX_VALUE;
     private List<PDDocument> newDocuments = null;
 
     /**
@@ -101,6 +103,53 @@ public class Splitter
     }
 
     /**
+     * This will set the start page.
+     * 
+     * @param start the start page
+     */
+    public void setStartPage( int start )
+    {
+        if( start <= 0 )
+        {
+            throw new RuntimeException( "Error split must be at least one page." );
+        }
+        startPage = start;
+    }
+    /**
+     * This will return the start page.
+     *
+     * @return The start page.
+     */
+    public int getStartPage()
+    {
+        return startPage;
+    }
+
+    /**
+     * This will set the end page.
+     * 
+     * @param end the end page
+     */
+    public void setEndPage( int end )
+    {
+        if( end <= 0 )
+        {
+            throw new RuntimeException( "Error split must be at least one page." );
+        }
+        endPage = end;
+    }
+    
+    /**
+     * This will return the end page.
+     *
+     * @return The end page.
+     */
+    public int getEndPage()
+    {
+        return endPage;
+    }
+
+    /**
      * Interface method to handle the start of the page processing.
      *
      * @param pages The list of pages from the source document.
@@ -113,7 +162,21 @@ public class Splitter
         while( iter.hasNext() )
         {
             PDPage page = (PDPage)iter.next();
-            processNextPage( page );
+            if (pageNumber+1 >= startPage && pageNumber+1 <= endPage)
+            {
+                processNextPage( page );
+            }
+            else
+            {
+                if (pageNumber > endPage)
+                {
+                    break;
+                }
+                else
+                {
+                    pageNumber++;
+                }
+            }
         }
     }