PDFAnnotationRedaction.java
12 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
/*
* 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.ASCoordinate
* com.adobe.internal.pdftoolkit.core.types.ASMatrix
* com.adobe.internal.pdftoolkit.core.types.ASName
* com.adobe.internal.pdftoolkit.core.types.ASString
*/
package com.adobe.internal.pdftoolkit.pdf.interactive.annotation;
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.ASCoordinate;
import com.adobe.internal.pdftoolkit.core.types.ASMatrix;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.types.ASString;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCosObject;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.graphics.PDFRectangle;
import com.adobe.internal.pdftoolkit.pdf.graphics.xobject.PDFXObjectForm;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationMarkup;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationPopup;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationUtils;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationWithQuadPoints;
import com.adobe.internal.pdftoolkit.pdf.utils.PDFUtil;
import java.util.List;
public class PDFAnnotationRedaction
extends PDFAnnotationMarkup
implements PDFAnnotationWithQuadPoints {
public static final ASName k_OVERLAYTEXT = ASName.create((String)"OverlayText");
public static final ASName k_RA = ASName.create((String)"RA");
public static final ASName k_REPEAT = ASName.create((String)"Repeat");
public static final ASName k_RO = ASName.create((String)"RO");
private PDFAnnotationRedaction(CosObject cosObject) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super(cosObject);
}
public static PDFAnnotationRedaction getInstance(CosObject cosObject) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFAnnotationRedaction pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFAnnotationRedaction.class);
if (pdfObject == null) {
pdfObject = new PDFAnnotationRedaction(cosObject);
}
return pdfObject;
}
private PDFAnnotationRedaction(PDFDocument pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
super(pdfDoc);
this.setSubtype(ASName.k_Redact);
}
public static PDFAnnotationRedaction newInstance(PDFDocument pdfDocument) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return new PDFAnnotationRedaction(pdfDocument);
}
public boolean hasQuadPoints() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_QuadPoints);
}
public double[] getQuadPoints() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosQuadPoints = this.getDictionaryArrayValue(ASName.k_QuadPoints);
if (cosQuadPoints == null) {
return null;
}
return cosQuadPoints.getArrayDouble();
}
public void setQuadPoints(double[] quadPoints) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (quadPoints == null) {
this.removeValue(ASName.k_QuadPoints);
} else {
CosArray cosArray = PDFCosObject.newCosArray(this.getPDFDocument());
for (int i = 0; i < quadPoints.length; ++i) {
cosArray.addDouble(quadPoints[i]);
}
this.setDictionaryArrayValue(ASName.k_QuadPoints, cosArray);
}
}
public void setQuadPoints(String points, String separator) throws PDFInvalidDocumentException, PDFIOException, PDFInvalidParameterException, PDFSecurityException {
this.setDictionaryArrayValue(ASName.k_QuadPoints, PDFUtil.parseNumbers(points, separator));
}
public void transformQuadPoints(ASMatrix matrix) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (this.hasQuadPoints()) {
double[] quadPoints = this.getQuadPoints();
double[] newQuagPoints = new double[quadPoints.length];
for (int i = 0; i < quadPoints.length; i += 2) {
ASCoordinate point = new ASCoordinate(quadPoints[i], quadPoints[i + 1]);
ASCoordinate newPoint = point.transform(matrix);
newQuagPoints[i] = newPoint.x();
newQuagPoints[i + 1] = newPoint.y();
}
this.setQuadPoints(newQuagPoints);
}
}
public boolean hasInteriorColor() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_IC);
}
public double[] getInteriorColor() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosColor = this.getDictionaryArrayValue(ASName.k_IC);
if (cosColor == null) {
return null;
}
return cosColor.getArrayDouble();
}
public void setInteriorColor(double red, double green, double blue) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosArray cosColor = PDFCosObject.newCosArray(this.getPDFDocument());
cosColor.addDouble(red);
cosColor.addDouble(green);
cosColor.addDouble(blue);
this.setDictionaryArrayValue(ASName.k_IC, cosColor);
}
public boolean hasOverlayText() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(k_OVERLAYTEXT);
}
public String getOverlayText() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryTextStringOrStreamValue(k_OVERLAYTEXT);
}
public void setOverlayText(String text) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryStringOrStreamValue(k_OVERLAYTEXT, text);
}
public boolean hasRepeat() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(k_REPEAT);
}
public boolean getRepeat() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryBooleanValue(k_REPEAT);
}
public void setRepeat(boolean repeat) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryBooleanValue(k_REPEAT, repeat);
}
public boolean hasDA() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_DA);
}
public String getDA() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
ASString asStr = this.getDictionaryStringValue(ASName.k_DA);
return asStr != null ? asStr.asString() : null;
}
public void setDA(String text) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryStringValue(ASName.k_DA, text);
}
public boolean hasRO() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(k_RO);
}
public PDFXObjectForm getRO() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return PDFXObjectForm.getInstance(this.getCosDictionary().get(k_RO));
}
public void setRO(PDFXObjectForm overlayAppearance) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryValue(k_RO, overlayAppearance);
}
public boolean hasJustification() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.dictionaryContains(ASName.k_Q);
}
public int getJustification() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryIntValue(ASName.k_Q);
}
public void setJustification(int justification) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryIntValue(ASName.k_Q, justification);
}
public void transform(ASMatrix matrix, double rotationAngle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.transformQuadPoints(matrix);
this.transformRect(matrix);
this.transformAppearances(matrix, rotationAngle);
}
private PDFRectangle getRedactionBBoxForQuadPoint(double[] quadPoint) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
double maxY;
double maxX;
double minX = maxX = quadPoint[0];
double minY = maxY = quadPoint[1];
for (int i = 2; i < quadPoint.length; i += 2) {
if (quadPoint[i] > maxX) {
maxX = quadPoint[i];
} else if (quadPoint[i] < minX) {
minX = quadPoint[i];
}
if (quadPoint[i + 1] > maxY) {
maxY = quadPoint[i + 1];
continue;
}
if (quadPoint[i + 1] >= minY) continue;
minY = quadPoint[i + 1];
}
return PDFRectangle.newInstance(this.getPDFDocument(), minX, minY, maxX, maxY);
}
public PDFRectangle getRedactionAreaBBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
double[] quadPoints;
PDFRectangle rectangle = null;
if (this.hasQuadPoints() && (quadPoints = this.getQuadPoints()) != null && quadPoints.length > 0 && quadPoints.length % 8 == 0) {
rectangle = this.getRedactionBBoxForQuadPoint(this.getQuadPoints());
}
if (rectangle == null) {
rectangle = this.getRect();
}
return rectangle;
}
public void applyRotation(PDFRectangle cropBox, int rotationAngle) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
if (rotationAngle == 0) {
rotationAngle = this.getRotation();
}
double height = cropBox.height();
double width = cropBox.width();
double[] transformedRect = null;
double[] transformedPopupRect = null;
double[] transformedQuadPoints = null;
PDFAnnotationPopup popup = this.getPopup();
transformedRect = PDFAnnotationUtils.transfromRectangle(this.getRect().getValues(), width, height, rotationAngle);
transformedQuadPoints = PDFAnnotationUtils.transfromRectangle(this.getQuadPoints(), width, height, rotationAngle);
if (popup != null) {
transformedPopupRect = PDFAnnotationUtils.transfromRectangle(this.getPopup().getRect().getValues(), width, height, rotationAngle);
}
this.setRect(PDFRectangle.newInstance(this.getPDFDocument(), transformedRect[0], transformedRect[1], transformedRect[2], transformedRect[3]));
this.setQuadPoints(transformedQuadPoints);
if (popup != null) {
this.getPopup().setRect(PDFRectangle.newInstance(this.getPDFDocument(), transformedPopupRect[0], transformedPopupRect[1], transformedPopupRect[2], transformedPopupRect[3]));
}
}
}