PDFPageTree.java
8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosArray
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.util.ArrayListStack
*/
package com.adobe.internal.pdftoolkit.pdf.page;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTreeList;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageTreeNode;
import com.adobe.internal.util.ArrayListStack;
import java.util.Iterator;
public class PDFPageTree
extends PDFPageTreeNode {
private PDFPageTree(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
public static PDFPageTree getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFPageTree pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFPageTree.class);
if (pdfObject == null) {
pdfObject = new PDFPageTree(cosObject);
}
return pdfObject;
}
public static PDFPageTree requireInstance(CosObject cosObject) throws PDFInvalidParameterException, PDFInvalidDocumentException {
PDFPageTree pdfObject = PDFPageTree.getInstance(cosObject);
if (pdfObject == null) {
throw new PDFInvalidParameterException("Required instance unable to be constructed.");
}
return pdfObject;
}
public static PDFPageTree newInstance(PDFDocument pdfDoc, PDFPage firstPage) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray treeList = PDFCosObject.newCosArray(pdfDoc);
treeList.add(firstPage.getCosObject());
CosDictionary cosObject = PDFCosObject.newCosDictionary(pdfDoc);
cosObject.put(ASName.k_Type, ASName.k_Pages);
cosObject.put(ASName.k_Kids, (CosObject)treeList);
cosObject.put(ASName.k_Count, 1);
pdfDoc.requireCatalog().getCosDictionary().put(ASName.k_Pages, (CosObject)cosObject);
PDFPageTree pageTree = new PDFPageTree((CosObject)cosObject);
firstPage.setDictionaryValue(ASName.k_Parent, pageTree);
return pageTree;
}
private PDFPage findPageInNode(PDFPageTreeNode node, int index, int least) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (index < least) {
return null;
}
int count = node.getCount() + least;
if (index >= count) {
return null;
}
return this.findPageInList(node.getKids(), index, least);
}
private PDFPage findPageInList(PDFPageTreeList list, int index, int least) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
for (int i = 0; i < list.size(); ++i) {
Object obj = list.get(i);
if (obj instanceof PDFPage) {
if (least == index) {
return (PDFPage)obj;
}
++least;
continue;
}
PDFPage page = this.findPageInNode((PDFPageTreeNode)obj, index, least);
if (page != null) {
return page;
}
least += ((PDFPageTreeNode)obj).getCount();
}
return null;
}
public PDFPage getLastPage() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayListStack nodeIndexStack = new ArrayListStack();
PDFPageTreeNode right = this;
while (!(right instanceof PDFPage)) {
PDFPageTreeList kidList = right.getKids();
int nodeIndex = kidList.size() - 1;
while (nodeIndex < 0) {
if (nodeIndexStack.isEmpty()) {
throw new PDFInvalidDocumentException("No pages in the document's page tree.");
}
right = right.getParent();
nodeIndex = (Integer)nodeIndexStack.pop() - 1;
kidList = right.getKids();
}
nodeIndexStack.push((Object)nodeIndex);
right = (PDFPageTreeNode)kidList.get(nodeIndex);
}
return (PDFPage)right;
}
public PDFPage getPage(int index) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getPageInNode(this.getPDFDocument().requirePages(), 0, index);
}
private PDFPage getPageInNode(PDFPageTreeNode node, int start, int pageNum) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int last = start + node.getCount();
if (pageNum >= last) {
return null;
}
for (PDFPageTreeNode curNode : node.getKids()) {
if (curNode instanceof PDFPage) {
if (start == pageNum) {
return (PDFPage)curNode;
}
++start;
continue;
}
PDFPage curPage = this.getPageInNode(curNode, start, pageNum);
if (curPage != null) {
return curPage;
}
start += curNode.getCount();
}
return null;
}
public void removePage(PDFPage page) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Iterator<PDFPage> pages = this.iterator();
while (pages.hasNext()) {
if (pages.next() != page) continue;
pages.remove();
}
}
public int size() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCount();
}
public int getNumPages() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCount();
}
public boolean isEmpty() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getCount() == 0;
}
public Object get(int index) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getPage(index);
}
public Object[] toArray() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
Object[] allPages = new Object[this.size()];
for (int i = 0; i < this.size(); ++i) {
allPages[i] = this.get(i);
}
return allPages;
}
public boolean movePage(Integer nPage, Integer nAfter) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFPage page;
boolean prependPage = false;
if (nAfter == null) {
nAfter = this.getLastPage().getPageIndex() - 1;
} else {
if (nAfter.equals(nPage)) {
return true;
}
if (nAfter == -1) {
prependPage = true;
} else if (nPage < nAfter) {
Integer n = nAfter;
Integer n2 = nAfter = Integer.valueOf(nAfter - 1);
}
}
if (nPage != null && (page = this.getPage(nPage)) != null) {
this.removePage(page);
if (prependPage) {
this.getPage(0).prependPage(page);
} else {
PDFPage appendAfterPage = this.getPage(nAfter);
if (appendAfterPage != null) {
appendAfterPage.appendPage(page);
}
}
return true;
}
return false;
}
}