Guest User

PDFSplit for Apache PDFBox 1.8

a guest
Feb 15th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.50 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ru.elibsystem.elis.utils.pdf;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.FileOutputStream;
  11.  
  12. import java.util.List;
  13.  
  14. import org.apache.pdfbox.exceptions.COSVisitorException;
  15.  
  16. import org.apache.pdfbox.pdmodel.PDDocument;
  17.  
  18. import org.apache.pdfbox.pdfwriter.COSWriter;
  19.  
  20. import org.apache.pdfbox.util.Splitter;
  21.  
  22. /**
  23.  * @author <a href="[email protected]">Ben Litchfield</a>
  24.  * @version $Revision: 1.6 $
  25.  * patcher @author Arsen I. Borovinskiy <[email protected]>
  26.  */
  27. public class PDFSplit {
  28.     private static final String PASSWORD = "-password";
  29.     private static final String SPLIT = "-split";
  30.     private static final String START_PAGE = "-startPage";
  31.     private static final String END_PAGE = "-endPage";
  32.     private static final String NONSEQ = "-nonSeq";
  33.     private static final String OUTPUT_PREFIX = "-outputPrefix";
  34.  
  35.     private PDFSplit()
  36.     {
  37.     }
  38.     /**
  39.      * Infamous main method.
  40.      *
  41.      * @param args Command line arguments, should be one and a reference to a file.
  42.      *
  43.      * @throws Exception If there is an error parsing the document.
  44.      */
  45.     public static void main( String[] args ) throws Exception
  46.     {
  47.         PDFSplit split = new PDFSplit();
  48.         split.split( args );
  49.     }
  50.  
  51.     private void split( String[] args ) throws Exception
  52.     {
  53.         String password = "";
  54.         String split = null;
  55.         String startPage = null;
  56.         String endPage = null;
  57.         boolean useNonSeqParser = false;
  58.         Splitter splitter = new Splitter();
  59.         String pdfFile = null;
  60.         String outputPrefix = null;
  61.         for( int i=0; i<args.length; i++ )
  62.         {
  63.             if( args[i].equals( PASSWORD ) )
  64.             {
  65.                 i++;
  66.                 if( i >= args.length )
  67.                 {
  68.                     usage();
  69.                 }
  70.                 password = args[i];
  71.             }
  72.             else if( args[i].equals( SPLIT ) )
  73.             {
  74.                 i++;
  75.                 if( i >= args.length )
  76.                 {
  77.                     usage();
  78.                 }
  79.                 split = args[i];
  80.             }
  81.             else if( args[i].equals( START_PAGE ) )
  82.             {
  83.                 i++;
  84.                 if( i >= args.length )
  85.                 {
  86.                     usage();
  87.                 }
  88.                 startPage = args[i];
  89.             }
  90.             else if( args[i].equals( END_PAGE ) )
  91.             {
  92.                 i++;
  93.                 if( i >= args.length )
  94.                 {
  95.                     usage();
  96.                 }
  97.                 endPage = args[i];
  98.             }
  99.             else if( args[i].equals( OUTPUT_PREFIX ) )
  100.             {
  101.                 i++;
  102.                 outputPrefix = args[i];
  103.             }
  104.             else if( args[i].equals( NONSEQ ) )
  105.             {
  106.                 useNonSeqParser = true;
  107.             }
  108.             else
  109.             {
  110.                 if( pdfFile == null )
  111.                 {
  112.                     pdfFile = args[i];
  113.                 }
  114.             }
  115.         }
  116.  
  117.         if( pdfFile == null )
  118.         {
  119.             usage();
  120.         }
  121.         else
  122.         {          
  123.             if(outputPrefix == null)
  124.             {
  125.                 outputPrefix = pdfFile.substring( 0, pdfFile.lastIndexOf( '.' ));
  126.             }
  127.            
  128.             PDDocument document = null;
  129.             List<PDDocument> documents = null;
  130.             try
  131.             {  
  132.                 if (useNonSeqParser)
  133.                 {
  134.                     document = PDDocument.loadNonSeq(new File(pdfFile), null, password);
  135.                 }
  136.                 else
  137.                 {
  138.                     document = PDDocument.load(pdfFile);
  139.                     if( document.isEncrypted() )
  140.                     {
  141.                         document.decrypt( password );
  142.                     }
  143.                 }
  144.  
  145.                 int numberOfPages = document.getNumberOfPages();
  146.                 boolean startEndPageSet = false;
  147.                 if (startPage != null)
  148.                 {
  149.                     splitter.setStartPage(Integer.parseInt( startPage ));
  150.                     startEndPageSet = true;
  151.                     if (split == null)
  152.                     {
  153.                         splitter.setSplitAtPage(numberOfPages);
  154.                     }
  155.                 }
  156.                 if (endPage != null)
  157.                 {
  158.                     splitter.setEndPage(Integer.parseInt( endPage ));
  159.                     startEndPageSet = true;
  160.                     if (split == null)
  161.                     {
  162.                         splitter.setSplitAtPage(Integer.parseInt( endPage ));
  163.                     }
  164.                 }
  165.                 if (split != null)
  166.                 {
  167.                     splitter.setSplitAtPage( Integer.parseInt( split ) );
  168.                 }
  169.                 else
  170.                 {
  171.                     if (!startEndPageSet)
  172.                     {
  173.                         splitter.setSplitAtPage(1);
  174.                     }
  175.                 }
  176.                    
  177.                 documents = splitter.split( document );
  178.                 for( int i=0; i<documents.size(); i++ )
  179.                 {
  180.                     PDDocument doc = documents.get( i );
  181.                     //String fileName = pdfFile.substring(0, pdfFile.length()-4 ) + "-" + i + ".pdf";
  182.                     String fileName = outputPrefix + "-" + (i+1) + ".pdf";
  183.                     writeDocument( doc, fileName );
  184.                     doc.close();
  185.                 }
  186.  
  187.             }
  188.             finally
  189.             {
  190.                 if( document != null )
  191.                 {
  192.                     document.close();
  193.                 }
  194.                 for( int i=0; documents != null && i<documents.size(); i++ )
  195.                 {
  196.                     PDDocument doc = (PDDocument)documents.get( i );
  197.                     doc.close();
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.     private static final void writeDocument( PDDocument doc, String fileName ) throws IOException, COSVisitorException
  204.     {
  205.         FileOutputStream output = null;
  206.         COSWriter writer = null;
  207.         try
  208.         {
  209.             output = new FileOutputStream( fileName );
  210.             writer = new COSWriter( output );
  211.             writer.write( doc );
  212.         }
  213.         finally
  214.         {
  215.             if( output != null )
  216.             {
  217.                 output.close();
  218.             }
  219.             if( writer != null )
  220.             {
  221.                 writer.close();
  222.             }
  223.         }
  224.     }
  225.  
  226.     /**
  227.      * This will print the usage requirements and exit.
  228.      */
  229.     private static void usage()
  230.     {
  231.         System.err.println( "Usage: java -jar pdfbox-app-x.y.z.jar PDFSplit [OPTIONS] <PDF file>\n" +
  232.             "  -password  <password>  Password to decrypt document\n" +
  233.             "  -split     <integer>   split after this many pages (default 1, if startPage and endPage are unset)\n"+
  234.             "  -startPage <integer>   start page\n" +
  235.             "  -endPage   <integer>   end page\n" +
  236.             "  -nonSeq                Enables the new non-sequential parser\n" +
  237.             "  -outputPrefix <output prefix>  Filename prefix for image files\n" +    
  238.             "  <PDF file>             The PDF document to use\n"
  239.             );
  240.         System.exit( 1 );
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment