PDFBookmark.java
11.6 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* 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.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.interactive.navigation;
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.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.interactive.action.PDFAction;
import com.adobe.internal.pdftoolkit.pdf.interactive.action.PDFActionFactory;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFBookmarkNode;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFBookmarkRoot;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFBookmarkUtils;
import com.adobe.internal.pdftoolkit.pdf.interactive.navigation.PDFDestination;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureElement;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPageMode;
import java.util.ArrayList;
public class PDFBookmark
extends PDFBookmarkNode {
private PDFBookmark(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
public static PDFBookmark getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFBookmark pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFBookmark.class);
if (pdfObject == null) {
pdfObject = new PDFBookmark(cosObject);
}
return pdfObject;
}
public static PDFBookmark newSkeletonInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new PDFBookmark((CosObject)PDFCosObject.newCosDictionary(pdfDocument));
}
public static PDFBookmark newInstance(PDFDocument pdfDocument, String title) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBookmark bookmark = PDFBookmark.newSkeletonInstance(pdfDocument);
bookmark.setTitle(title);
return bookmark;
}
public boolean hasTitle() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_Title);
}
public void setTitle(String title) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryStringValue(ASName.k_Title, title);
}
public String getTitle() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringValue(ASName.k_Title);
}
public void setDestination(PDFDestination destination) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(ASName.k_Dest, destination);
}
public void setAction(PDFAction action) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (!this.dictionaryContains(ASName.k_Dest)) {
this.setDictionaryValue(ASName.k_A, action);
}
}
public boolean hasColor() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_C);
}
public double[] getColor() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (!this.dictionaryContains(ASName.k_C)) {
return null;
}
return this.getDictionaryArrayValue(ASName.k_C).getArrayDouble();
}
public void setColor(float red, float green, float blue) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setColor((double)red, (double)green, (double)blue);
}
public void setColor(double red, double green, double blue) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
red = Math.max(0.0, Math.min(1.0, red));
green = Math.max(0.0, Math.min(1.0, green));
blue = Math.max(0.0, Math.min(1.0, blue));
this.setDictionaryArrayValue(ASName.k_C, PDFCosObject.makeCosArray(this.getPDFDocument(), 0, new double[]{red, green, blue}, 0, 2));
}
public boolean hasStyles() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_F);
}
public int getStyles() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryIntValue(ASName.k_F);
}
public void setStyles(int f) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryIntValue(ASName.k_F, f);
}
public void setParent(PDFBookmarkNode bookmark) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(ASName.k_Parent, bookmark);
}
public void setPrevious(PDFBookmark bookmark) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(ASName.k_Prev, bookmark);
}
public void setNext(PDFBookmark bookmark) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(ASName.k_Next, bookmark);
}
public PDFBookmark getPrevious() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFBookmark.getInstance(this.getDictionaryCosObjectValue(ASName.k_Prev));
}
public PDFBookmark getNext() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFBookmark.getInstance(this.getDictionaryCosObjectValue(ASName.k_Next));
}
public ArrayList getChildren() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ArrayList list = new ArrayList();
PDFBookmark kid = this.getFirstKid();
if (kid != null) {
list.add((PDFBookmark)kid);
list = this.getNextKid(kid, list);
} else {
list = this.getNextKid(this, list);
}
return list;
}
private ArrayList getNextKid(PDFBookmark kid, ArrayList list) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
while (kid.getNext() != null) {
list.add(kid.getNext());
kid = kid.getNext();
}
return list;
}
public PDFBookmarkNode getParent() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBookmarkRoot root = this.getPDFDocument().requireCatalog().getBookmarkRoot();
CosObject parent = this.getDictionaryCosObjectValue(ASName.k_Parent);
if (parent == root.getCosObject()) {
return PDFBookmarkRoot.getInstance(parent);
}
return PDFBookmark.getInstance(parent);
}
public PDFBookmark newLastKid(String title) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBookmark bookmark = PDFBookmark.newInstance(this.getPDFDocument(), title);
PDFBookmarkUtils.appendLastKid(bookmark, this);
PDFBookmarkUtils.appendLastKid(bookmark, this);
return bookmark;
}
public PDFDestination getDestination() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFDestination.getInstance(this.getDictionaryCosObjectValue(ASName.k_Dest));
}
public PDFAction getAction() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFActionFactory.getInstance((CosObject)this.getDictionaryDictionaryValue(ASName.k_A));
}
public PDFBookmarkNode delete() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBookmark prev = this.getPrevious();
PDFBookmark next = this.getNext();
PDFBookmarkNode parent = this.getParent();
if (prev == null) {
if (next == null) {
if (parent instanceof PDFBookmarkRoot) {
PDFCatalog catalog = parent.getPDFDocument().requireCatalog();
catalog.removeValue(ASName.k_Outlines);
PDFPageMode pageMode = catalog.getPageMode();
if (pageMode == PDFPageMode.WithBookmarks) {
catalog.setPageMode(PDFPageMode.PagesOnly);
}
return null;
}
parent.removeValue(ASName.k_First);
parent.removeValue(ASName.k_Last);
return parent;
}
parent.setFirst(next);
} else {
prev.setNext(next);
}
if (next == null) {
if (prev != null) {
parent.setLast(prev);
}
} else {
next.setPrevious(prev);
}
return prev != null ? prev : next;
}
public PDFStructureElement getStructureElement() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFStructureElement.getInstance(this.getDictionaryCosObjectValue(ASName.k_SE));
}
public void setStructureElement(PDFStructureElement element) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(ASName.k_SE, element);
}
public int getCount() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.getDictionaryIntValue(ASName.k_Count) != null) {
return this.getDictionaryIntValue(ASName.k_Count);
}
PDFBookmark kid = this.getFirstKid();
if (kid != null) {
throw new PDFInvalidDocumentException("Count Entry is missing at this node where it's required");
}
return 0;
}
public boolean hasAction() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getAction() != null;
}
public boolean hasDestination() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDestination() != null;
}
public boolean hasCount() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_Count);
}
public boolean hasStructureElement() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getStructureElement() != null;
}
public PDFBookmark findDepthFirst(String title) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFBookmark b;
if (title.equals(this.getTitle())) {
return this;
}
for (b = this.getFirstKid(); b != null; b = b.getNext()) {
PDFBookmark b2 = b.findDepthFirst(title);
if (b2 == null) continue;
return b2;
}
b = this.getNext();
if (b != null) {
return b.findDepthFirst(title);
}
return null;
}
}