acrofields.java

来自「源码包含生成 PDF 和 HTML 的类库」· Java 代码 · 共 1,451 行 · 第 1/5 页

JAVA
1,451
字号
                setField(f, v);            List l = xfdf.getListValues(f);            if (l != null)            	setListSelection(v, (String[])l.toArray());        }    }    /**     * Regenerates the field appearance.     * This is useful 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) {                //we'll assume that the value is an image in base64                Image img;                try {                    img = Image.getInstance(Base64.decode(value));                }                catch (Exception e) {                    return false;                }                PushbuttonField pb = getNewPushbuttonFromField(name);                pb.setImage(img);                replacePushbuttonField(name, pb.getField());                return true;            }            PdfName v = new PdfName(value);            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;    }        /**     * Sets different values in a list selection.     * No appearance is generated yet; nor does the code check if multiple select is allowed.     * @param	name	the name of the field     * @param	value	an array with values that need to be selected     * @return	true only if the field value was changed     * @since 2.1.4     */	public boolean setListSelection(String name, String[] value) throws IOException, DocumentException {        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.CH.equals(type)) {        	return false;        }        String[] options = getListOptionExport(name);        PdfArray array = new PdfArray();        for (int i = 0; i < value.length; i++) {        	for (int j = 0; j < options.length; j++) {        		if (options[j].equals(value[i])) {        			array.add(new PdfNumber(j));        		}        	}        }        for (int idx = 0; idx < item.values.size(); ++idx) {            PdfDictionary valueDic = (PdfDictionary)item.values.get(idx);            valueDic.remove(PdfName.V);            valueDic.put(PdfName.I, array);            markUsed(valueDic);                            PdfDictionary merged = (PdfDictionary)item.merged.get(idx);            merged.put(PdfName.I, array);            merged.remove(PdfName.V);            PdfDictionary widget = (PdfDictionary)item.widgets.get(idx);            widget.remove(PdfName.AP);            merged.remove(PdfName.AP);            markUsed(widget);        }        return true;	}        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 = PdfRe

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?