1 31 package org.pdfbox; 32 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.FileInputStream ; 36 import java.io.FileOutputStream ; 37 38 import java.util.List ; 39 40 import org.pdfbox.exceptions.InvalidPasswordException; 41 import org.pdfbox.exceptions.COSVisitorException; 42 43 import org.pdfbox.pdfparser.PDFParser; 44 45 import org.pdfbox.pdmodel.PDDocument; 46 47 import org.pdfbox.pdfwriter.COSWriter; 48 49 import org.pdfbox.util.Splitter; 50 51 58 public class PDFSplit 59 { 60 private static final String PASSWORD = "-password"; 61 private static final String SPLIT = "-split"; 62 63 70 public static void main( String [] args ) throws Exception 71 { 72 PDFSplit split = new PDFSplit(); 73 split.split( args ); 74 } 75 76 private void split( String [] args ) throws Exception 77 { 78 String password = ""; 79 String split = "1"; 80 81 Splitter splitter = new Splitter(); 82 String pdfFile = null; 83 for( int i=0; i<args.length; i++ ) 84 { 85 if( args[i].equals( PASSWORD ) ) 86 { 87 i++; 88 if( i >= args.length ) 89 { 90 usage(); 91 } 92 password = args[i]; 93 } 94 else if( args[i].equals( SPLIT ) ) 95 { 96 i++; 97 if( i >= args.length ) 98 { 99 usage(); 100 } 101 split = args[i]; 102 } 103 else 104 { 105 if( pdfFile == null ) 106 { 107 pdfFile = args[i]; 108 } 109 } 110 } 111 112 if( pdfFile == null ) 113 { 114 usage(); 115 } 116 else 117 { 118 119 InputStream input = null; 120 PDDocument document = null; 121 List documents = null; 122 try 123 { 124 input = new FileInputStream ( pdfFile ); 125 document = parseDocument( input ); 126 127 if( document.isEncrypted() ) 128 { 129 try 130 { 131 document.decrypt( password ); 132 } 133 catch( InvalidPasswordException e ) 134 { 135 if( args.length == 4 ) { 137 System.err.println( "Error: The supplied password is incorrect." ); 138 System.exit( 2 ); 139 } 140 else 141 { 142 System.err.println( "Error: The document is encrypted." ); 144 usage(); 145 } 146 } 147 } 148 149 splitter.setSplitAtPage( Integer.parseInt( split ) ); 150 documents = splitter.split( document ); 151 for( int i=0; i<documents.size(); i++ ) 152 { 153 PDDocument doc = (PDDocument)documents.get( i ); 154 String fileName = pdfFile.substring(0, pdfFile.length()-4 ) + "-" + i + ".pdf"; 155 writeDocument( doc, fileName ); 156 doc.close(); 157 } 158 159 } 160 finally 161 { 162 if( input != null ) 163 { 164 input.close(); 165 } 166 if( document != null ) 167 { 168 document.close(); 169 } 170 for( int i=0; documents != null && i<documents.size(); i++ ) 171 { 172 PDDocument doc = (PDDocument)documents.get( i ); 173 doc.close(); 174 } 175 } 176 } 177 } 178 179 private static final void writeDocument( PDDocument doc, String fileName ) throws IOException , COSVisitorException 180 { 181 FileOutputStream output = null; 182 COSWriter writer = null; 183 try 184 { 185 output = new FileOutputStream ( fileName ); 186 writer = new COSWriter( output ); 187 writer.write( doc ); 188 } 189 finally 190 { 191 if( output != null ) 192 { 193 output.close(); 194 } 195 if( writer != null ) 196 { 197 writer.close(); 198 } 199 } 200 } 201 202 211 private static PDDocument parseDocument( InputStream input )throws IOException 212 { 213 PDFParser parser = new PDFParser( input ); 214 parser.parse(); 215 return parser.getPDDocument(); 216 } 217 218 221 private static void usage() 222 { 223 System.err.println( "Usage: java org.pdfbox.PDFSplit [OPTIONS] <PDF file>\n" + 224 " -password <password> Password to decrypt document\n" + 225 " -split <integer> split after this many pages\n" + 226 " <PDF file> The PDF document to use\n" 227 ); 228 System.exit( 1 ); 229 } 230 } | Popular Tags |