001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox;
031:
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.FileInputStream;
035: import java.io.FileOutputStream;
036:
037: import java.util.List;
038:
039: import org.pdfbox.exceptions.InvalidPasswordException;
040: import org.pdfbox.exceptions.COSVisitorException;
041:
042: import org.pdfbox.pdfparser.PDFParser;
043:
044: import org.pdfbox.pdmodel.PDDocument;
045:
046: import org.pdfbox.pdfwriter.COSWriter;
047:
048: import org.pdfbox.util.Splitter;
049:
050: /**
051: * This is the main program that will take a pdf document and split it into
052: * a number of other documents.
053: *
054: * @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
055: * @version $Revision: 1.6 $
056: */
057: public class PDFSplit {
058: private static final String PASSWORD = "-password";
059: private static final String SPLIT = "-split";
060:
061: /**
062: * Infamous main method.
063: *
064: * @param args Command line arguments, should be one and a reference to a file.
065: *
066: * @throws Exception If there is an error parsing the document.
067: */
068: public static void main(String[] args) throws Exception {
069: PDFSplit split = new PDFSplit();
070: split.split(args);
071: }
072:
073: private void split(String[] args) throws Exception {
074: String password = "";
075: String split = "1";
076:
077: Splitter splitter = new Splitter();
078: String pdfFile = null;
079: for (int i = 0; i < args.length; i++) {
080: if (args[i].equals(PASSWORD)) {
081: i++;
082: if (i >= args.length) {
083: usage();
084: }
085: password = args[i];
086: } else if (args[i].equals(SPLIT)) {
087: i++;
088: if (i >= args.length) {
089: usage();
090: }
091: split = args[i];
092: } else {
093: if (pdfFile == null) {
094: pdfFile = args[i];
095: }
096: }
097: }
098:
099: if (pdfFile == null) {
100: usage();
101: } else {
102:
103: InputStream input = null;
104: PDDocument document = null;
105: List documents = null;
106: try {
107: input = new FileInputStream(pdfFile);
108: document = parseDocument(input);
109:
110: if (document.isEncrypted()) {
111: try {
112: document.decrypt(password);
113: } catch (InvalidPasswordException e) {
114: if (args.length == 4)//they supplied the wrong password
115: {
116: System.err
117: .println("Error: The supplied password is incorrect.");
118: System.exit(2);
119: } else {
120: //they didn't suppply a password and the default of "" was wrong.
121: System.err
122: .println("Error: The document is encrypted.");
123: usage();
124: }
125: }
126: }
127:
128: splitter.setSplitAtPage(Integer.parseInt(split));
129: documents = splitter.split(document);
130: for (int i = 0; i < documents.size(); i++) {
131: PDDocument doc = (PDDocument) documents.get(i);
132: String fileName = pdfFile.substring(0, pdfFile
133: .length() - 4)
134: + "-" + i + ".pdf";
135: writeDocument(doc, fileName);
136: doc.close();
137: }
138:
139: } finally {
140: if (input != null) {
141: input.close();
142: }
143: if (document != null) {
144: document.close();
145: }
146: for (int i = 0; documents != null
147: && i < documents.size(); i++) {
148: PDDocument doc = (PDDocument) documents.get(i);
149: doc.close();
150: }
151: }
152: }
153: }
154:
155: private static final void writeDocument(PDDocument doc,
156: String fileName) throws IOException, COSVisitorException {
157: FileOutputStream output = null;
158: COSWriter writer = null;
159: try {
160: output = new FileOutputStream(fileName);
161: writer = new COSWriter(output);
162: writer.write(doc);
163: } finally {
164: if (output != null) {
165: output.close();
166: }
167: if (writer != null) {
168: writer.close();
169: }
170: }
171: }
172:
173: /**
174: * This will parse a document.
175: *
176: * @param input The input stream for the document.
177: *
178: * @return The document.
179: *
180: * @throws IOException If there is an error parsing the document.
181: */
182: private static PDDocument parseDocument(InputStream input)
183: throws IOException {
184: PDFParser parser = new PDFParser(input);
185: parser.parse();
186: return parser.getPDDocument();
187: }
188:
189: /**
190: * This will print the usage requirements and exit.
191: */
192: private static void usage() {
193: System.err
194: .println("Usage: java org.pdfbox.PDFSplit [OPTIONS] <PDF file>\n"
195: + " -password <password> Password to decrypt document\n"
196: + " -split <integer> split after this many pages\n"
197: + " <PDF file> The PDF document to use\n");
198: System.exit(1);
199: }
200: }
|