PDFFieldButton.java
9.51 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.cos.CosDictionary
* com.adobe.internal.pdftoolkit.core.cos.CosName
* 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.exceptions.PDFUnableToCompleteOperationException
* com.adobe.internal.pdftoolkit.core.types.ASName
*/
package com.adobe.internal.pdftoolkit.pdf.interactive.forms;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
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.exceptions.PDFUnableToCompleteOperationException;
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.graphics.PDFRectangle;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotation;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationIterator;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAnnotationWidget;
import com.adobe.internal.pdftoolkit.pdf.interactive.annotation.PDFAppearance;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFDefaultAppearance;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldNode;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldUtils;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFFieldWithOptions;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.internal.pdftoolkit.pdf.page.PDFPage;
public class PDFFieldButton
extends PDFFieldWithOptions {
static final int kNoToggleToOff = 16384;
static final int kRadioButton = 32768;
static final int kPushButton = 65536;
static final int kRadiosInUnison = 33554432;
public static final ASName offState = ASName.create((String)"Off");
public static final ASName neutralState = ASName.create((String)"Neutral");
private static final String DEFAULT_BUTTONNAME = "Button";
public static final ASName DEFAULT_ON_STATE_NAME = ASName.create((String)"Yes");
private PDFFieldButton(CosObject cosObject) throws PDFInvalidDocumentException {
super(cosObject);
}
public static PDFFieldButton newInstance(String qualifiedFieldName, PDFPage page, PDFRectangle annotRect, PDFDefaultAppearance da, boolean combineFieldAnnot) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
if (page == null) {
throw new PDFInvalidParameterException("A page must be provided for the annotation when creating fields.");
}
if (annotRect == null) {
throw new PDFInvalidParameterException("An annotation rectangle must be specified when creating fields.");
}
CosDictionary cosObject = PDFCosObject.newCosDictionary(page.getPDFDocument());
PDFFieldButton field = new PDFFieldButton((CosObject)cosObject);
PDFInteractiveForm iform = page.getPDFDocument().requireCatalog().procureInteractiveForm();
PDFFieldNode parent = iform.procureIntermediateFieldNodes(qualifiedFieldName);
String fieldName = PDFFieldUtils.getTerminalFieldName(qualifiedFieldName);
field.init(parent, fieldName, page, annotRect, combineFieldAnnot, da);
return field;
}
public static PDFFieldButton newInstance(PDFFieldNode parent, PDFPage page, PDFRectangle annotRect, PDFDefaultAppearance da, boolean combineFieldAnnot) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
if (parent == null) {
throw new PDFInvalidParameterException("A parent field node must be specified when creating fields.");
}
if (page == null) {
throw new PDFInvalidParameterException("A page must be provided for the annotation when creating fields.");
}
if (annotRect == null) {
throw new PDFInvalidParameterException("An annotation rectangle must be specified when creating fields.");
}
CosDictionary cosObject = PDFCosObject.newCosDictionary(page.getPDFDocument());
PDFFieldButton field = new PDFFieldButton((CosObject)cosObject);
field.init(parent, null, page, annotRect, combineFieldAnnot, da);
return field;
}
private void init(PDFFieldNode parent, String fieldName, PDFPage page, PDFRectangle annotRect, boolean combineFieldAnnot, PDFDefaultAppearance da) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException, PDFInvalidParameterException {
this.init(parent, fieldName, "Button", da, false);
PDFAnnotationWidget widget = this.initWidgetAnnot(page, annotRect, combineFieldAnnot);
this.initButtonAttributes(widget);
}
private void initButtonAttributes(PDFAnnotationWidget widget) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.setDictionaryNameValue(ASName.k_FT, ASName.k_Btn);
this.setNameValue(ASName.create((String)"Yes"));
widget.setAppearanceState(true);
}
public static PDFFieldButton getInstance(CosObject cosObject) throws PDFInvalidDocumentException {
if (PDFCosObject.checkNullCosObject(cosObject) == null) {
return null;
}
PDFFieldButton pdfObject = PDFCosObject.getCachedInstance(cosObject, PDFFieldButton.class);
if (pdfObject == null) {
pdfObject = new PDFFieldButton(cosObject);
}
return pdfObject;
}
public boolean isPushButton() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return (super.getFlags() & 65536) != 0;
}
public boolean isCheckBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return !this.isPushButton() && !this.isRadioButton();
}
public boolean isRadioButton() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return (super.getFlags() & 32768) != 0;
}
public void setRadioButton() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int flg = this.getFlags();
this.setFlags(flg |= 32768);
}
public void setPushButon() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int flg = this.getFlags();
this.setFlags(flg |= 65536);
}
public void setCheckBox() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
int flags = this.getFlags();
int mask = -32769;
flags &= mask;
mask = -65537;
this.setFlags(flags &= mask);
}
public boolean isRadiosInUnison() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return (this.getFlags() & 33554432) != 0;
}
public void setRadiosInUnison(boolean value) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
if (value) {
int flg = this.getFlags();
this.setFlags(flg |= 33554432);
} else {
int flags = this.getFlags();
int mask = -33554433;
this.setFlags(flags &= mask);
}
}
public Object getValue() throws PDFUnableToCompleteOperationException, PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
return this.getDictionaryNameValue(ASName.k_AS).asString();
}
public void setStateFromValue() throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
CosObject values = this.getCosDictionary().get(ASName.k_V);
if (values == null || !(values instanceof CosName) || this.isPushButton()) {
return;
}
PDFAnnotationIterator widgetsIterator = this.getAnnotationsIterator();
ASName onState = ((CosName)values).nameValue();
boolean oneTime = false;
if (widgetsIterator != null) {
while (widgetsIterator.hasNext()) {
PDFAnnotationWidget widget = (PDFAnnotationWidget)widgetsIterator.next();
PDFAppearance widgetAppearance = widget.getAppearance();
CosObject normalAppearance = widgetAppearance.getCosDictionary().get(ASName.k_N);
if (normalAppearance == null) {
normalAppearance = widgetAppearance.getCosDictionary().get(ASName.k_D);
}
if (normalAppearance == null || normalAppearance.getType() != 6) continue;
widget.setDictionaryNameValue(ASName.k_AS, offState);
if (!((CosDictionary)normalAppearance).containsKey((Object)onState) || oneTime) continue;
widget.setDictionaryNameValue(ASName.k_AS, onState);
if (!this.isRadioButton() || this.isRadiosInUnison()) continue;
oneTime = true;
}
}
}
}