KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfPages


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

50
51 package com.lowagie.text.pdf;
52
53 import java.io.IOException JavaDoc;
54 import java.util.ArrayList JavaDoc;
55
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 JavaDoc pages = new ArrayList JavaDoc();
73     private ArrayList JavaDoc parents = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc e) {
111             throw new ExceptionConverter(e);
112         }
113     }
114     
115     // returns the top parent to include in the catalog
116
PdfIndirectReference writePageTree() throws IOException JavaDoc {
117         if (pages.isEmpty())
118             throw new IOException JavaDoc("The document has no pages.");
119         int leaf = 1;
120         ArrayList JavaDoc tParents = parents;
121         ArrayList JavaDoc tPages = pages;
122         ArrayList JavaDoc nextParents = new ArrayList JavaDoc();
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 JavaDoc 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                 writer.addToBody(top, (PdfIndirectReference)tParents.get(p));
152             }
153             if (tParents.size() == 1) {
154                 topParent = (PdfIndirectReference)tParents.get(0);
155                 return topParent;
156             }
157             tPages = tParents;
158             tParents = nextParents;
159             nextParents = new ArrayList JavaDoc();
160         }
161     }
162     
163     PdfIndirectReference getTopParent() {
164         return topParent;
165     }
166     
167     void setLinearMode(PdfIndirectReference topParent) {
168         if (parents.size() > 1)
169             throw new RuntimeException JavaDoc("Linear page mode can only be called with a single parent.");
170         if (topParent != null) {
171             this.topParent = topParent;
172             parents.clear();
173             parents.add(topParent);
174         }
175         leafSize = 10000000;
176     }
177
178     void addPage(PdfIndirectReference page) {
179         pages.add(page);
180     }
181
182     int reorderPages(int order[]) throws DocumentException {
183         if (order == null)
184             return pages.size();
185         if (parents.size() > 1)
186             throw new DocumentException("Page reordering requires a single parent in the page tree. Call PdfWriter.setLinearMode() after open.");
187         if (order.length != pages.size())
188             throw new DocumentException("Page reordering requires an array with the same size as the number of pages.");
189         int max = pages.size();
190         boolean temp[] = new boolean[max];
191         for (int k = 0; k < max; ++k) {
192             int p = order[k];
193             if (p < 1 || p > max)
194                 throw new DocumentException("Page reordering requires pages between 1 and " + max + ". Found " + p + ".");
195             if (temp[p - 1])
196                 throw new DocumentException("Page reordering requires no page repetition. Page " + p + " is repeated.");
197             temp[p - 1] = true;
198         }
199         Object JavaDoc copy[] = pages.toArray();
200         for (int k = 0; k < max; ++k) {
201             pages.set(k, copy[order[k] - 1]);
202         }
203         return max;
204     }
205 }
Popular Tags