1 /*
2  * $Id: PdfPages.java 3934 2009-05-27 11:23:23Z blowagie $
3  *
4  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf;
51
52 import java.io.IOException;
53 import java.util.ArrayList;
54
55 import com.lowagie.text.Document;
56 import com.lowagie.text.DocumentException;
57 import com.lowagie.text.ExceptionConverter;
58
59 /**
60  * <CODE>PdfPages</CODE> is the PDF Pages-object.
61  * <P>
62  * The Pages of a document are accessible through a tree of nodes known as the Pages tree.
63  * This tree defines the ordering of the pages in the document.<BR>
64  * This object is described in the 'Portable Document Format Reference Manual version 1.3'
65  * section 6.3 (page 71-73)
66  *
67  * @see        PdfPage
68  */

69
70 public class PdfPages {
71     
72     private ArrayList pages = new ArrayList();
73     private ArrayList parents = new ArrayList();
74     private int leafSize = 10;
75     private PdfWriter writer;
76     private PdfIndirectReference topParent;
77     
78     // constructors
79     
80 /**
81  * Constructs a <CODE>PdfPages</CODE>-object.
82  */

83     
84     PdfPages(PdfWriter writer) {
85         this.writer = writer;
86     }
87     
88     void addPage(PdfDictionary page) {
89         try {
90             if ((pages.size() % leafSize) == 0)
91                 parents.add(writer.getPdfIndirectReference());
92             PdfIndirectReference parent = (PdfIndirectReference)parents.get(parents.size() - 1);
93             page.put(PdfName.PARENT, parent);
94             PdfIndirectReference current = writer.getCurrentPage();
95             writer.addToBody(page, current);
96             pages.add(current);
97         }
98         catch (Exception e) {
99             throw new ExceptionConverter(e);
100         }
101     }
102     
103     PdfIndirectReference addPageRef(PdfIndirectReference pageRef) {
104         try {
105             if ((pages.size() % leafSize) == 0)
106                 parents.add(writer.getPdfIndirectReference());
107             pages.add(pageRef);
108             return (PdfIndirectReference)parents.get(parents.size() - 1);
109         }
110         catch (Exception e) {
111             throw new ExceptionConverter(e);
112         }
113     }
114     
115     // returns the top parent to include in the catalog
116     PdfIndirectReference writePageTree() throws IOException {
117         if (pages.isEmpty())
118             throw new IOException("The document has no pages.");
119         int leaf = 1;
120         ArrayList tParents = parents;
121         ArrayList tPages = pages;
122         ArrayList nextParents = new ArrayList();
123         while (true) {
124             leaf *= leafSize;
125             int stdCount = leafSize;
126             int rightCount = tPages.size() % leafSize;
127             if (rightCount == 0)
128                 rightCount = leafSize;
129             for (int p = 0; p < tParents.size(); ++p) {
130                 int count;
131                 int thisLeaf = leaf;
132                 if (p == tParents.size() - 1) {
133                     count = rightCount;
134                     thisLeaf = pages.size() % leaf;
135                     if (thisLeaf == 0)
136                         thisLeaf = leaf;
137                 }
138                 else
139                     count = stdCount;
140                 PdfDictionary top = new PdfDictionary(PdfName.PAGES);
141                 top.put(PdfName.COUNT, new PdfNumber(thisLeaf));
142                 PdfArray kids = new PdfArray();
143                 ArrayList internal = kids.getArrayList();
144                 internal.addAll(tPages.subList(p * stdCount, p * stdCount + count));
145                 top.put(PdfName.KIDS, kids);
146                 if (tParents.size() > 1) {
147                     if ((p % leafSize) == 0)
148                         nextParents.add(writer.getPdfIndirectReference());
149                     top.put(PdfName.PARENT, (PdfIndirectReference)nextParents.get(p / leafSize));
150                 }
151                 else {
152                     top.put(PdfName.ITXT, new PdfString(Document.getRelease()));
153                 }
154                 writer.addToBody(top, (PdfIndirectReference)tParents.get(p));
155             }
156             if (tParents.size() == 1) {
157                 topParent = (PdfIndirectReference)tParents.get(0);
158                 return topParent;
159             }
160             tPages = tParents;
161             tParents = nextParents;
162             nextParents = new ArrayList();
163         }
164     }
165     
166     PdfIndirectReference getTopParent() {
167         return topParent;
168     }
169     
170     void setLinearMode(PdfIndirectReference topParent) {
171         if (parents.size() > 1)
172             throw new RuntimeException("Linear page mode can only be called with a single parent.");
173         if (topParent != null) {
174             this.topParent = topParent;
175             parents.clear();
176             parents.add(topParent);
177         }
178         leafSize = 10000000;
179     }
180
181     void addPage(PdfIndirectReference page) {
182         pages.add(page);
183     }
184
185     int reorderPages(int order[]) throws DocumentException {
186         if (order == null)
187             return pages.size();
188         if (parents.size() > 1)
189             throw new DocumentException("Page reordering requires a single parent in the page tree. Call PdfWriter.setLinearMode() after open.");
190         if (order.length != pages.size())
191             throw new DocumentException("Page reordering requires an array with the same size as the number of pages.");
192         int max = pages.size();
193         boolean temp[] = new boolean[max];
194         for (int k = 0; k < max; ++k) {
195             int p = order[k];
196             if (p < 1 || p > max)
197                 throw new DocumentException("Page reordering requires pages between 1 and " + max + ". Found " + p + ".");
198             if (temp[p - 1])
199                 throw new DocumentException("Page reordering requires no page repetition. Page " + p + " is repeated.");
200             temp[p - 1] = true;
201         }
202         Object copy[] = pages.toArray();
203         for (int k = 0; k < max; ++k) {
204             pages.set(k, copy[order[k] - 1]);
205         }
206         return max;
207     }
208 }