KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > PDFSplit


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox;
32
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.FileInputStream JavaDoc;
36 import java.io.FileOutputStream JavaDoc;
37
38 import java.util.List JavaDoc;
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 /**
52  * This is the main program that will take a pdf document and split it into
53  * a number of other documents.
54  *
55  * @author <a HREF="ben@benlitchfield.com">Ben Litchfield</a>
56  * @version $Revision: 1.6 $
57  */

58 public class PDFSplit
59 {
60     private static final String JavaDoc PASSWORD = "-password";
61     private static final String JavaDoc SPLIT = "-split";
62
63     /**
64      * Infamous main method.
65      *
66      * @param args Command line arguments, should be one and a reference to a file.
67      *
68      * @throws Exception If there is an error parsing the document.
69      */

70     public static void main( String JavaDoc[] args ) throws Exception JavaDoc
71     {
72         PDFSplit split = new PDFSplit();
73         split.split( args );
74     }
75
76     private void split( String JavaDoc[] args ) throws Exception JavaDoc
77     {
78         String JavaDoc password = "";
79         String JavaDoc split = "1";
80
81         Splitter splitter = new Splitter();
82         String JavaDoc 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 JavaDoc input = null;
120             PDDocument document = null;
121             List JavaDoc documents = null;
122             try
123             {
124                 input = new FileInputStream JavaDoc( 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 )//they supplied the wrong password
136
{
137                             System.err.println( "Error: The supplied password is incorrect." );
138                             System.exit( 2 );
139                         }
140                         else
141                         {
142                             //they didn't suppply a password and the default of "" was wrong.
143
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 JavaDoc 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 JavaDoc fileName ) throws IOException JavaDoc, COSVisitorException
180     {
181         FileOutputStream JavaDoc output = null;
182         COSWriter writer = null;
183         try
184         {
185             output = new FileOutputStream JavaDoc( 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     /**
203      * This will parse a document.
204      *
205      * @param input The input stream for the document.
206      *
207      * @return The document.
208      *
209      * @throws IOException If there is an error parsing the document.
210      */

211     private static PDDocument parseDocument( InputStream JavaDoc input )throws IOException JavaDoc
212     {
213         PDFParser parser = new PDFParser( input );
214         parser.parse();
215         return parser.getPDDocument();
216     }
217
218     /**
219      * This will print the usage requirements and exit.
220      */

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