PDFStructureUtils.java
10.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* 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
*/
package com.adobe.internal.pdftoolkit.pdf.interchange.structure;
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.PDFCosDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentArray;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentInterface;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureContentIterator;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureElement;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureNode;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureOBJR;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureParentTree;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureRole;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureRoleMap;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureRoot;
import com.adobe.internal.pdftoolkit.pdf.interchange.structure.PDFStructureType;
public class PDFStructureUtils {
public static PDFStructureRole getRole(PDFStructureElement structElement) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureRoot structRoot = structElement.getPDFDocument().requireCatalog().getStructureRoot();
PDFStructureRoleMap roleMap = structRoot.getRoleMap();
ASName role = null;
if (roleMap != null) {
role = roleMap.getRole(structElement.requireStructureType());
}
if (role == null) {
return PDFStructureRole.getRole(structElement.requireStructureType());
}
return PDFStructureRole.getRole(role);
}
public static void removeOBJReference(PDFCosDictionary refDict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFStructureElement parentStructElement = PDFStructureUtils.getStructParentElement(refDict);
if (parentStructElement == null) {
return;
}
PDFStructureContentInterface parentContent = parentStructElement.getContent();
if (parentContent == null) {
return;
}
PDFStructureContentIterator contentIter = parentContent.contentIterator();
while (contentIter.hasNext()) {
PDFStructureParentTree parentTree;
PDFStructureContentInterface curContent = contentIter.next();
if (!curContent.getStructureType().contains(PDFStructureType.OBJR) || refDict.getCosObject() != ((PDFStructureOBJR)curContent).getDictionaryValue(ASName.k_Obj)) continue;
if (contentIter.isScalar()) {
parentStructElement.removeKids();
} else {
contentIter.remove();
}
PDFStructureRoot structRoot = refDict.getPDFDocument().requireCatalog().getStructureRoot();
PDFStructureParentTree pDFStructureParentTree = parentTree = structRoot == null ? null : structRoot.getParentTree();
if (parentTree == null) break;
parentTree.removeEntry(PDFStructureUtils.getStructParent(refDict));
break;
}
}
public static void replaceOBJR(PDFCosDictionary newRef, PDFCosDictionary oldRef) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (PDFStructureUtils.hasStructParent(oldRef)) {
int structParent = PDFStructureUtils.getStructParent(oldRef);
PDFStructureElement parentElement = PDFStructureUtils.getStructParentElement(oldRef);
if (parentElement != null) {
PDFStructureContentInterface parentContent = parentElement.getContent();
PDFStructureContentIterator contentIter = parentContent.contentIterator();
while (contentIter.hasNext()) {
PDFStructureOBJR objrContent;
PDFStructureContentInterface curContent = contentIter.next();
if (!curContent.getStructureType().contains(PDFStructureType.OBJR) || !(objrContent = (PDFStructureOBJR)curContent).containsOBJ(oldRef)) continue;
oldRef.removeValue(ASName.k_StructParent);
PDFStructureUtils.setStructParent(structParent, newRef);
objrContent.setOBJR(newRef);
}
}
}
}
public static PDFStructureNode getNode(CosDictionary node) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASName type = null;
if (node.containsKey((Object)ASName.k_Type)) {
type = node.getName(ASName.k_Type);
}
if (type != null && type.equals((Object)ASName.k_StructTreeRoot)) {
return PDFStructureRoot.getInstance(node);
}
return PDFStructureElement.getInstance(node);
}
public static PDFStructureContentArray getStructParentArray(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureParentTree parentTree;
if (!PDFStructureUtils.hasStructParents(dict)) {
return null;
}
PDFStructureRoot structRoot = dict.getPDFDocument().requireCatalog().getStructureRoot();
PDFStructureParentTree pDFStructureParentTree = parentTree = structRoot == null ? null : structRoot.getParentTree();
if (parentTree == null) {
return null;
}
PDFStructureContentInterface parentArray = (PDFStructureContentInterface)parentTree.getEntry(PDFStructureUtils.getStructParents(dict));
if (parentArray == null) {
return null;
}
if (!(parentArray instanceof PDFStructureContentArray)) {
throw new PDFInvalidDocumentException("Expected PDFStructureContentArray");
}
return (PDFStructureContentArray)parentArray;
}
public static int getStructParent(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return dict.getDictionaryIntValue(ASName.k_StructParent);
}
public static void setStructParent(int structParent, PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
dict.setDictionaryIntValue(ASName.k_StructParent, structParent);
}
public static boolean hasStructParent(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
boolean result = false;
if (dict.dictionaryContains(ASName.k_StructParent)) {
result = true;
}
return result;
}
public static Integer getStructParents(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return dict.getDictionaryIntValue(ASName.k_StructParents);
}
public static void setStructParents(Integer structParents, PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (structParents == null) {
dict.removeValue(ASName.k_StructParents);
} else {
dict.setDictionaryIntValue(ASName.k_StructParents, structParents.intValue());
}
}
public static boolean hasStructParents(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
boolean result = false;
if (dict.dictionaryContains(ASName.k_StructParents)) {
result = true;
}
return result;
}
public static PDFStructureElement getStructParentElement(PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFStructureParentTree parentTree;
if (!PDFStructureUtils.hasStructParent(dict)) {
return null;
}
PDFStructureRoot structRoot = dict.getPDFDocument().requireCatalog().getStructureRoot();
PDFStructureParentTree pDFStructureParentTree = parentTree = structRoot == null ? null : structRoot.getParentTree();
if (parentTree == null) {
return null;
}
PDFStructureContentInterface parentElement = (PDFStructureContentInterface)parentTree.getEntry(PDFStructureUtils.getStructParent(dict));
if (parentElement == null) {
return null;
}
if (!(parentElement instanceof PDFStructureElement)) {
throw new PDFInvalidDocumentException("Expected PDFStructureElement");
}
return (PDFStructureElement)parentElement;
}
private static int setStructParents(PDFStructureContentInterface structParents, PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFStructureRoot structRoot = dict.getPDFDocument().requireCatalog().getStructureRoot();
if (structRoot == null) {
return -1;
}
PDFStructureParentTree parentTree = structRoot.procureParentTree();
int key = structRoot.getNextParentKey();
parentTree.addEntry(key, structParents);
structRoot.setNextParentKey(key + 1);
return key;
}
public static void setStructParentsInTree(PDFStructureContentArray structParents, PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFStructureUtils.setStructParents(PDFStructureUtils.setStructParents(structParents, dict), dict);
}
public static void setStructParentInTree(PDFStructureElement structParent, PDFCosDictionary dict) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
PDFStructureUtils.setStructParents(PDFStructureUtils.setStructParents(structParent, dict), dict);
}
}