ModifiableContent.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.io.stream.InputByteStream
* com.adobe.internal.io.stream.StreamManager
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosObject
* com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
* 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.contentmodify;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
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.content.Content;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFContents;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosDictionary;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosDictionaryMap;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.document.PDFResources;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFExtGState;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFExtGStateMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpace;
import com.adobe.internal.pdftoolkit.pdf.graphics.colorspaces.PDFColorSpaceMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFont;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFFontMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.patterns.PDFPattern;
import com.adobe.internal.pdftoolkit.pdf.graphics.patterns.PDFPatternMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.patterns.PDFPatternTiling;
import com.adobe.internal.pdftoolkit.pdf.graphics.shading.PDFShading;
import com.adobe.internal.pdftoolkit.pdf.graphics.shading.PDFShadingMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObject;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectForm;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectImage;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectMap;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFDefaultAppearance;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.pdf.interactive.markedcontent.PDFMCProperty;
import com.adobe.internal.pdftoolkit.pdf.interactive.markedcontent.PDFMCPropertyMap;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
import java.io.IOException;
import java.util.Map;
public class ModifiableContent
extends Content {
private static final String xFormName = "Fm";
private static final String xImageName = "Im";
private ModifiableContent(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super(pdfDocument);
}
private ModifiableContent(PDFContents contents, boolean contentDetached, PDFResources resources, boolean resourcesDetached) {
super(contents, contentDetached, resources, resourcesDetached);
}
public static ModifiableContent newInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new ModifiableContent(pdfDocument);
}
public static ModifiableContent newInstance(PDFContents contents, PDFResources resources) {
return new ModifiableContent(contents, false, resources, false);
}
public ASName addResource(PDFColorSpace colorspace) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "Cs";
PDFColorSpaceMap map = this.resources.procureColorSpaceMap();
return this.procureResourceName(map, colorspace, baseName);
}
public ASName addResource(PDFExtGState extGState) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "GS";
PDFExtGStateMap map = this.resources.procureExtGStateMap();
return this.procureResourceName(map, extGState, baseName);
}
public ASName addResource(PDFFont font) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "F";
PDFFontMap map = this.resources.procureFontMap();
return this.procureResourceName(map, font, baseName);
}
public ASName addResource(PDFMCProperty property) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "P";
PDFMCPropertyMap map = this.resources.procureMCPropertyMap();
return this.procureResourceName(map, property, baseName);
}
public ASName addResource(PDFPattern pattern) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "P";
PDFPatternMap map = this.resources.procurePatternMap();
return this.procureResourceName(map, pattern.getPDFCosObject(), baseName);
}
public ASName addResource(PDFShading shading) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "Sh";
PDFShadingMap map = this.resources.procureShadingMap();
return this.procureResourceName(map, shading.getPDFCosObject(), baseName);
}
public ASName addResource(PDFXObject pdfXObject) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = null;
if (pdfXObject instanceof PDFXObjectForm) {
baseName = "Fm";
} else if (pdfXObject instanceof PDFXObjectImage) {
baseName = "Im";
}
PDFXObjectMap map = this.resources.procureXObjectMap();
return this.procureResourceName(map, pdfXObject, baseName);
}
ASName addResource(PDFXObjectImage xObjectImage) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
String baseName = "Im";
PDFXObjectMap map = this.resources.procureXObjectMap();
return this.procureResourceName(map, xObjectImage, baseName);
}
public static ModifiableContent getInstance(PDFContents contents, PDFResources resources) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new ModifiableContent(contents, false, resources, false);
}
public static ModifiableContent newInstance(PDFPatternTiling pattern) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFContents contents = pattern.getContents();
PDFResources resources = pattern.getResources();
boolean contentDetached = false;
boolean resourcesDetached = false;
if (contents == null) {
contents = PDFContents.newInstance(pattern.getPDFDocument());
contentDetached = true;
}
if (resources == null) {
resources = PDFResources.newInstance(pattern.getPDFDocument());
resourcesDetached = true;
}
return new ModifiableContent(contents, contentDetached, resources, resourcesDetached);
}
public static ModifiableContent newInstance(PDFPage page) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFContents contents = page.getContents();
PDFResources resources = page.getResources();
boolean contentDetached = false;
boolean resourcesDetached = false;
if (contents == null) {
contents = PDFContents.newInstance(page.getPDFDocument());
contentDetached = true;
}
if (resources == null) {
resources = PDFResources.newInstance(page.getPDFDocument());
resourcesDetached = true;
}
return new ModifiableContent(contents, contentDetached, resources, resourcesDetached);
}
public static ModifiableContent newInstance(PDFXObjectForm xobjectForm) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
PDFContents contents = xobjectForm.getContents();
PDFResources resources = xobjectForm.getResources();
boolean contentDetached = false;
boolean resourcesDetached = false;
if (contents == null) {
contents = PDFContents.newInstance(xobjectForm.getPDFDocument());
contentDetached = true;
}
if (resources == null) {
resources = PDFResources.newInstance(xobjectForm.getPDFDocument());
resourcesDetached = true;
}
return new ModifiableContent(contents, contentDetached, resources, resourcesDetached);
}
public static ModifiableContent newInstance(PDFDefaultAppearance defaultAppearance) throws PDFSecurityException, PDFIOException, PDFInvalidDocumentException {
try {
if (defaultAppearance == null) {
return null;
}
byte[] bytes = defaultAppearance.asByteArray();
if (bytes != null) {
PDFResources resources;
PDFDocument pdfDocument = defaultAppearance.getPDFDocument();
boolean contentDetached = false;
boolean resourcesDetached = false;
PDFContents contents = PDFContents.newInstance(pdfDocument);
contents.setContents(defaultAppearance.getStreamManager().getInputByteStream(bytes));
PDFInteractiveForm acroForm = pdfDocument.requireCatalog().getInteractiveForm();
if (acroForm != null) {
resources = acroForm.getResources();
} else {
resources = PDFResources.newInstance(pdfDocument);
resourcesDetached = true;
}
return new ModifiableContent(contents, contentDetached, resources, resourcesDetached);
}
return null;
}
catch (IOException e) {
throw new PDFIOException((Throwable)e);
}
}
private ASName procureResourceName(PDFCosDictionaryMap resourceMap, PDFCosObject resource, String baseName) throws PDFIOException, PDFInvalidDocumentException, PDFSecurityException {
ASName resourceName = ModifiableContent.getExistingResourceName(resourceMap, resource.getCosObject());
if (resourceName == null) {
resourceName = this.makeResourceName(resourceMap, baseName);
resourceMap.put(resourceName, resource);
}
return resourceName;
}
private static ASName getExistingResourceName(PDFCosDictionaryMap resourceMap, CosObject resource) throws PDFCosParseException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosDictionary cosDict = resourceMap.getCosDictionary();
return cosDict.getKeyForValue(resource);
}
private ASName makeResourceName(Map map, String baseName) {
int i = 0;
ASName resourceName = null;
while (map.containsKey((Object)(resourceName = ASName.create((String)(baseName + Integer.toString(++i)))))) {
}
return resourceName;
}
public void setContents(PDFContents contents) {
this.contents = contents;
}
}