KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > util > Splitter


1 /**
2  * Copyright (c) 2004-2005, 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.util;
32
33 import org.pdfbox.pdmodel.PDDocument;
34 import org.pdfbox.pdmodel.PDPage;
35
36 import java.io.IOException JavaDoc;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * Split a document into several other documents.
44  *
45  * @author Mario Ivankovits (mario@ops.co.at)
46  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
47  * @version $Revision: 1.7 $
48  */

49 public class Splitter
50 {
51
52     /**
53      * The source PDF document.
54      */

55     protected PDDocument pdfDocument;
56     
57     /**
58      * The current PDF document that contains the splitted page.
59      */

60     protected PDDocument currentDocument = null;
61     
62     private int splitAtPage=1;
63     private List JavaDoc newDocuments = null;
64
65     /**
66      * The current page number that we are processing, zero based.
67      */

68     protected int pageNumber = 0;
69
70     /**
71      * This will take a document and split into several other documents.
72      *
73      * @param document The document to split.
74      *
75      * @return A list of all the split documents.
76      *
77      * @throws IOException If there is an IOError
78      */

79     public List JavaDoc split( PDDocument document ) throws IOException JavaDoc
80     {
81         newDocuments = new ArrayList JavaDoc();
82         pdfDocument = document;
83
84         List JavaDoc pages = pdfDocument.getDocumentCatalog().getAllPages();
85         processPages(pages);
86         return newDocuments;
87     }
88
89     /**
90      * This will tell the splitting algorithm where to split the pages. The default
91      * is 1, so every page will become a new document. If it was to then each document would
92      * contain 2 pages. So it the source document had 5 pages it would split into
93      * 3 new documents, 2 documents containing 2 pages and 1 document containing one
94      * page.
95      *
96      * @param split The number of pages each split document should contain.
97      */

98     public void setSplitAtPage( int split )
99     {
100         if( split <= 0 )
101         {
102             throw new RuntimeException JavaDoc( "Error split must be at least one page." );
103         }
104         splitAtPage = split;
105     }
106
107     /**
108      * This will return how many pages each split document will contain.
109      *
110      * @return The split parameter.
111      */

112     public int getSplitAtPage()
113     {
114         return splitAtPage;
115     }
116
117     /**
118      * Interface method to handle the start of the page processing.
119      *
120      * @param pages The list of pages from the source document.
121      *
122      * @throws IOException If an IO error occurs.
123      */

124     protected void processPages(List JavaDoc pages) throws IOException JavaDoc
125     {
126         Iterator JavaDoc iter = pages.iterator();
127         while( iter.hasNext() )
128         {
129             PDPage page = (PDPage)iter.next();
130             processNextPage( page );
131         }
132     }
133     
134     /**
135      * Interface method, you can control where a document gets split by implementing
136      * this method. By default a split occurs at every page. If you wanted to split
137      * based on some complex logic then you could override this method. For example.
138      * <code>
139      * protected void createNewDocumentIfNecessary()
140      * {
141      * if( isPrime( pageNumber ) )
142      * {
143      * super.createNewDocumentIfNecessary();
144      * }
145      * }
146      * </code>
147      *
148      * @throws IOException If there is an error creating the new document.
149      */

150     protected void createNewDocumentIfNecessary() throws IOException JavaDoc
151     {
152         if (isNewDocNecessary())
153         {
154             createNewDocument();
155         }
156     }
157     
158     /**
159      * Check if it is necessary to create a new document.
160      *
161      * @return true If a new document should be created.
162      */

163     protected boolean isNewDocNecessary()
164     {
165         return pageNumber % splitAtPage == 0 || currentDocument == null;
166     }
167     
168     /**
169      * Create a new document to write the splitted contents to.
170      *
171      * @throws IOException If there is an problem creating the new document.
172      */

173     protected void createNewDocument() throws IOException JavaDoc
174     {
175         currentDocument = new PDDocument();
176         currentDocument.setDocumentInformation(pdfDocument.getDocumentInformation());
177         currentDocument.getDocumentCatalog().setViewerPreferences(
178         pdfDocument.getDocumentCatalog().getViewerPreferences());
179         newDocuments.add(currentDocument);
180     }
181
182
183
184     /**
185      * Interface to start processing a new page.
186      *
187      * @param page The page that is about to get processed.
188      *
189      * @throws IOException If there is an error creating the new document.
190      */

191     protected void processNextPage( PDPage page ) throws IOException JavaDoc
192     {
193         createNewDocumentIfNecessary();
194         PDPage imported = currentDocument.importPage( page );
195         imported.setCropBox( page.findCropBox() );
196         imported.setMediaBox( page.findMediaBox() );
197         imported.setResources( page.findResources() );
198         imported.setRotation( page.findRotation() );
199         pageNumber++;
200     }
201 }
Popular Tags