📄 acrofields.java
字号:
public void setFields(XfdfReader xfdf) throws IOException, DocumentException {
HashMap fd = xfdf.getFields();
for (Iterator i = fd.keySet().iterator(); i.hasNext();) {
String f = (String)i.next();
String v = xfdf.getFieldValue(f);
if (v != null)
setField(f, v);
}
}
/**
* Regenerates the field appearance.
* This is usefull when you change a field property, but not its value,
* for instance form.setFieldProperty("f", "bgcolor", Color.BLUE, null);
* This won't have any effect, unless you use regenerateField("f") after changing
* the property.
*
* @param name the fully qualified field name or the partial name in the case of XFA forms
* @throws IOException on error
* @throws DocumentException on error
* @return <CODE>true</CODE> if the field was found and changed,
* <CODE>false</CODE> otherwise
*/
public boolean regenerateField(String name) throws IOException, DocumentException {
String value = getField(name);
return setField(name, value, value);
}
/** Sets the field value.
* @param name the fully qualified field name or the partial name in the case of XFA forms
* @param value the field value
* @throws IOException on error
* @throws DocumentException on error
* @return <CODE>true</CODE> if the field was found and changed,
* <CODE>false</CODE> otherwise
*/
public boolean setField(String name, String value) throws IOException, DocumentException {
return setField(name, value, null);
}
/** Sets the field value and the display string. The display string
* is used to build the appearance in the cases where the value
* is modified by Acrobat with JavaScript and the algorithm is
* known.
* @param name the fully qualified field name or the partial name in the case of XFA forms
* @param value the field value
* @param display the string that is used for the appearance. If <CODE>null</CODE>
* the <CODE>value</CODE> parameter will be used
* @return <CODE>true</CODE> if the field was found and changed,
* <CODE>false</CODE> otherwise
* @throws IOException on error
* @throws DocumentException on error
*/
public boolean setField(String name, String value, String display) throws IOException, DocumentException {
if (writer == null)
throw new DocumentException("This AcroFields instance is read-only.");
if (xfa.isXfaPresent()) {
name = xfa.findFieldName(name, this);
if (name == null)
return false;
String shortName = XfaForm.Xml2Som.getShortName(name);
Node xn = xfa.findDatasetsNode(shortName);
if (xn == null) {
xn = xfa.getDatasetsSom().insertNode(xfa.getDatasetsNode(), shortName);
}
xfa.setNodeText(xn, value);
}
Item item = (Item)fields.get(name);
if (item == null)
return false;
PdfName type = (PdfName)PdfReader.getPdfObject(((PdfDictionary)item.merged.get(0)).get(PdfName.FT));
if (PdfName.TX.equals(type)) {
PdfNumber maxLen = (PdfNumber)PdfReader.getPdfObject(((PdfDictionary)item.merged.get(0)).get(PdfName.MAXLEN));
int len = 0;
if (maxLen != null)
len = maxLen.intValue();
if (len > 0)
value = value.substring(0, Math.min(len, value.length()));
}
if (display == null)
display = value;
if (PdfName.TX.equals(type) || PdfName.CH.equals(type)) {
PdfString v = new PdfString(value, PdfObject.TEXT_UNICODE);
for (int idx = 0; idx < item.values.size(); ++idx) {
PdfDictionary valueDic = (PdfDictionary)item.values.get(idx);
valueDic.put(PdfName.V, v);
valueDic.remove(PdfName.I);
markUsed(valueDic);
PdfDictionary merged = (PdfDictionary)item.merged.get(idx);
merged.remove(PdfName.I);
merged.put(PdfName.V, v);
PdfDictionary widget = (PdfDictionary)item.widgets.get(idx);
if (generateAppearances) {
PdfAppearance app = getAppearance(merged, display, name);
if (PdfName.CH.equals(type)) {
PdfNumber n = new PdfNumber(topFirst);
widget.put(PdfName.TI, n);
merged.put(PdfName.TI, n);
}
PdfDictionary appDic = (PdfDictionary)PdfReader.getPdfObject(widget.get(PdfName.AP));
if (appDic == null) {
appDic = new PdfDictionary();
widget.put(PdfName.AP, appDic);
merged.put(PdfName.AP, appDic);
}
appDic.put(PdfName.N, app.getIndirectReference());
writer.releaseTemplate(app);
}
else {
widget.remove(PdfName.AP);
merged.remove(PdfName.AP);
}
markUsed(widget);
}
return true;
}
else if (PdfName.BTN.equals(type)) {
PdfNumber ff = (PdfNumber)PdfReader.getPdfObject(((PdfDictionary)item.merged.get(0)).get(PdfName.FF));
int flags = 0;
if (ff != null)
flags = ff.intValue();
if ((flags & PdfFormField.FF_PUSHBUTTON) != 0)
return true;
PdfName v = new PdfName(value);
if ((flags & PdfFormField.FF_RADIO) == 0) {
for (int idx = 0; idx < item.values.size(); ++idx) {
((PdfDictionary)item.values.get(idx)).put(PdfName.V, v);
markUsed((PdfDictionary)item.values.get(idx));
PdfDictionary merged = (PdfDictionary)item.merged.get(idx);
merged.put(PdfName.V, v);
merged.put(PdfName.AS, v);
PdfDictionary widget = (PdfDictionary)item.widgets.get(idx);
if (isInAP(widget, v))
widget.put(PdfName.AS, v);
else
widget.put(PdfName.AS, PdfName.Off);
markUsed(widget);
}
}
else {
ArrayList lopt = new ArrayList();
PdfObject opts = PdfReader.getPdfObject(((PdfDictionary)item.values.get(0)).get(PdfName.OPT));
if (opts != null && opts.isArray()) {
ArrayList list = ((PdfArray)opts).getArrayList();
for (int k = 0; k < list.size(); ++k) {
PdfObject vv = PdfReader.getPdfObject((PdfObject)list.get(k));
if (vv != null && vv.isString())
lopt.add(((PdfString)vv).toUnicodeString());
else
lopt.add(null);
}
}
int vidx = lopt.indexOf(value);
PdfName valt = null;
PdfName vt;
if (vidx >= 0) {
vt = valt = new PdfName(String.valueOf(vidx));
}
else
vt = v;
for (int idx = 0; idx < item.values.size(); ++idx) {
PdfDictionary merged = (PdfDictionary)item.merged.get(idx);
PdfDictionary widget = (PdfDictionary)item.widgets.get(idx);
markUsed((PdfDictionary)item.values.get(idx));
if (valt != null) {
PdfString ps = new PdfString(value, PdfObject.TEXT_UNICODE);
((PdfDictionary)item.values.get(idx)).put(PdfName.V, ps);
merged.put(PdfName.V, ps);
}
else {
((PdfDictionary)item.values.get(idx)).put(PdfName.V, v);
merged.put(PdfName.V, v);
}
markUsed(widget);
if (isInAP(widget, vt)) {
merged.put(PdfName.AS, vt);
widget.put(PdfName.AS, vt);
}
else {
merged.put(PdfName.AS, PdfName.Off);
widget.put(PdfName.AS, PdfName.Off);
}
}
}
return true;
}
return false;
}
boolean isInAP(PdfDictionary dic, PdfName check) {
PdfDictionary appDic = (PdfDictionary)PdfReader.getPdfObject(dic.get(PdfName.AP));
if (appDic == null)
return false;
PdfDictionary NDic = (PdfDictionary)PdfReader.getPdfObject(appDic.get(PdfName.N));
return (NDic != null && NDic.get(check) != null);
}
/** Gets all the fields. The fields are keyed by the fully qualified field name and
* the value is an instance of <CODE>AcroFields.Item</CODE>.
* @return all the fields
*/
public HashMap getFields() {
return fields;
}
/**
* Gets the field structure.
* @param name the name of the field
* @return the field structure or <CODE>null</CODE> if the field
* does not exist
*/
public Item getFieldItem(String name) {
if (xfa.isXfaPresent()) {
name = xfa.findFieldName(name, this);
if (name == null)
return null;
}
return (Item)fields.get(name);
}
/**
* Gets the long XFA translated name.
* @param name the name of the field
* @return the long field name
*/
public String getTranslatedFieldName(String name) {
if (xfa.isXfaPresent()) {
String namex = xfa.findFieldName(name, this);
if (namex != null)
name = namex;
}
return name;
}
/**
* Gets the field box positions in the document. The return is an array of <CODE>float</CODE>
* multiple of 5. For each of this groups the values are: [page, llx, lly, urx,
* ury]. The coordinates have the page rotation in consideration.
* @param name the field name
* @return the positions or <CODE>null</CODE> if field does not exist
*/
public float[] getFieldPositions(String name) {
Item item = getFieldItem(name);
if (item == null)
return null;
float ret[] = new float[item.page.size() * 5];
int ptr = 0;
for (int k = 0; k < item.page.size(); ++k) {
try {
PdfDictionary wd = (PdfDictionary)item.widgets.get(k);
PdfArray rect = (PdfArray)wd.get(PdfName.RECT);
if (rect == null)
continue;
Rectangle r = PdfReader.getNormalizedRectangle(rect);
int page = ((Integer)item.page.get(k)).intValue();
int rotation = reader.getPageRotation(page);
ret[ptr++] = page;
if (rotation != 0) {
Rectangle pageSize = reader.getPageSize(page);
switch (rotation) {
case 270:
r = new Rectangle(
pageSize.getTop() - r.getBottom(),
r.getLeft(),
pageSize.getTop() - r.getTop(),
r.getRight());
break;
case 180:
r = new Rectangle(
pageSize.getRight() - r.getLeft(),
pageSize.getTop() - r.getBottom(),
pageSize.getRight() - r.getRight(),
pageSize.getTop() - r.getTop());
break;
case 90:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -