textfield.java

来自「处理PDF」· Java 代码 · 共 663 行 · 第 1/2 页

JAVA
663
字号
            last = choices.length;        topFirst = first;        app.saveState();        app.rectangle(offsetX, offsetX, box.getWidth() - 2 * offsetX, box.getHeight() - 2 * offsetX);        app.clip();        app.newPath();        Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor;        app.setColorFill(new Color(10, 36, 106));        app.rectangle(offsetX, offsetX + h - (topChoice - first + 1) * leading, box.getWidth() - 2 * offsetX, leading);        app.fill();        float xp = offsetX * 2;        float yp = offsetX + h - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);        for (int idx = first; idx < last; ++idx, yp -= leading) {            String ptext = choices[idx];            int rtl = checkRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;            ptext = removeCRLF(ptext);            Phrase phrase = composePhrase(ptext, ufont, (idx == topChoice) ? GrayColor.GRAYWHITE : fcolor, usize);            ColumnText.showTextAligned(app, Element.ALIGN_LEFT, phrase, xp, yp, 0, rtl, 0);        }        app.restoreState();        app.endVariableText();        return app;    }    /** Gets a new text field.     * @throws IOException on error     * @throws DocumentException on error     * @return a new text field     */        public PdfFormField getTextField() throws IOException, DocumentException {        if (maxCharacterLength <= 0)            options &= ~COMB;        if ((options & COMB) != 0)            options &= ~MULTILINE;        PdfFormField field = PdfFormField.createTextField(writer, false, false, maxCharacterLength);        field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);        switch (alignment) {            case Element.ALIGN_CENTER:                field.setQuadding(PdfFormField.Q_CENTER);                break;            case Element.ALIGN_RIGHT:                field.setQuadding(PdfFormField.Q_RIGHT);                break;        }        if (rotation != 0)            field.setMKRotation(rotation);        if (fieldName != null) {            field.setFieldName(fieldName);            if ((options & REQUIRED) == 0 && !"".equals(text))            	field.setValueAsString(text);            if (defaultText != null)                field.setDefaultValueAsString(defaultText);            if ((options & READ_ONLY) != 0)                field.setFieldFlags(PdfFormField.FF_READ_ONLY);            if ((options & REQUIRED) != 0)                field.setFieldFlags(PdfFormField.FF_REQUIRED);            if ((options & MULTILINE) != 0)                field.setFieldFlags(PdfFormField.FF_MULTILINE);            if ((options & DO_NOT_SCROLL) != 0)                field.setFieldFlags(PdfFormField.FF_DONOTSCROLL);            if ((options & PASSWORD) != 0)                field.setFieldFlags(PdfFormField.FF_PASSWORD);            if ((options & FILE_SELECTION) != 0)                field.setFieldFlags(PdfFormField.FF_FILESELECT);            if ((options & DO_NOT_SPELL_CHECK) != 0)                field.setFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);            if ((options & COMB) != 0)                field.setFieldFlags(PdfFormField.FF_COMB);        }        field.setBorderStyle(new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3)));        PdfAppearance tp = getAppearance();        field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);        PdfAppearance da = (PdfAppearance)tp.getDuplicate();        da.setFontAndSize(getRealFont(), fontSize);        if (textColor == null)            da.setGrayFill(0);        else            da.setColorFill(textColor);        field.setDefaultAppearanceString(da);        if (borderColor != null)            field.setMKBorderColor(borderColor);        if (backgroundColor != null)            field.setMKBackgroundColor(backgroundColor);        switch (visibility) {            case HIDDEN:                field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN);                break;            case VISIBLE_BUT_DOES_NOT_PRINT:                break;            case HIDDEN_BUT_PRINTABLE:                field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW);                break;            default:                field.setFlags(PdfAnnotation.FLAGS_PRINT);                break;        }        return field;    }        /** Gets a new combo field.     * @throws IOException on error     * @throws DocumentException on error     * @return a new combo field     */        public PdfFormField getComboField() throws IOException, DocumentException {        return getChoiceField(false);    }        /** Gets a new list field.     * @throws IOException on error     * @throws DocumentException on error     * @return a new list field     */        public PdfFormField getListField() throws IOException, DocumentException {        return getChoiceField(true);    }    protected PdfFormField getChoiceField(boolean isList) throws IOException, DocumentException {        options &= (~MULTILINE) & (~COMB);        String uchoices[] = choices;        if (uchoices == null)            uchoices = new String[0];        int topChoice = choiceSelection;        if (topChoice >= uchoices.length)            topChoice = uchoices.length - 1;        if (text == null) text = ""; //fixed by Kazuya Ujihara (ujihara.jp)        if (topChoice >= 0)            text = uchoices[topChoice];        if (topChoice < 0)            topChoice = 0;        PdfFormField field = null;        String mix[][] = null;        if (choiceExports == null) {            if (isList)                field = PdfFormField.createList(writer, uchoices, topChoice);            else                field = PdfFormField.createCombo(writer, (options & EDIT) != 0, uchoices, topChoice);        }        else {            mix = new String[uchoices.length][2];            for (int k = 0; k < mix.length; ++k)                mix[k][0] = mix[k][1] = uchoices[k];            int top = Math.min(uchoices.length, choiceExports.length);            for (int k = 0; k < top; ++k) {                if (choiceExports[k] != null)                    mix[k][0] = choiceExports[k];            }            if (isList)                field = PdfFormField.createList(writer, mix, topChoice);            else                field = PdfFormField.createCombo(writer, (options & EDIT) != 0, mix, topChoice);        }        field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT);        if (rotation != 0)            field.setMKRotation(rotation);        if (fieldName != null) {            field.setFieldName(fieldName);            if (uchoices.length > 0) {                if (mix != null) {                    field.setValueAsString(mix[topChoice][0]);                    field.setDefaultValueAsString(mix[topChoice][0]);                }                else {                    field.setValueAsString(text);                    field.setDefaultValueAsString(text);                }            }            if ((options & READ_ONLY) != 0)                field.setFieldFlags(PdfFormField.FF_READ_ONLY);            if ((options & REQUIRED) != 0)                field.setFieldFlags(PdfFormField.FF_REQUIRED);            if ((options & DO_NOT_SPELL_CHECK) != 0)                field.setFieldFlags(PdfFormField.FF_DONOTSPELLCHECK);        }        field.setBorderStyle(new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3)));        PdfAppearance tp;        if (isList) {            tp = getListAppearance();            if (topFirst > 0)                field.put(PdfName.TI, new PdfNumber(topFirst));        }        else            tp = getAppearance();        field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);        PdfAppearance da = (PdfAppearance)tp.getDuplicate();        da.setFontAndSize(getRealFont(), fontSize);        if (textColor == null)            da.setGrayFill(0);        else            da.setColorFill(textColor);        field.setDefaultAppearanceString(da);        if (borderColor != null)            field.setMKBorderColor(borderColor);        if (backgroundColor != null)            field.setMKBackgroundColor(backgroundColor);        switch (visibility) {            case HIDDEN:                field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN);                break;            case VISIBLE_BUT_DOES_NOT_PRINT:                break;            case HIDDEN_BUT_PRINTABLE:                field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW);                break;            default:                field.setFlags(PdfAnnotation.FLAGS_PRINT);                break;        }        return field;    }        /** Gets the default text.     * @return the default text     */    public String getDefaultText() {        return this.defaultText;    }        /** Sets the default text. It is only meaningful for text fields.     * @param defaultText the default text     */    public void setDefaultText(String defaultText) {        this.defaultText = defaultText;    }        /** Gets the choices to be presented to the user in list/combo     * fields.     * @return the choices to be presented to the user     */    public String[] getChoices() {        return this.choices;    }        /** Sets the choices to be presented to the user in list/combo     * fields.     * @param choices the choices to be presented to the user     */    public void setChoices(String[] choices) {        this.choices = choices;    }        /** Gets the export values in list/combo fields.     * @return the export values in list/combo fields     */    public String[] getChoiceExports() {        return this.choiceExports;    }        /** Sets the export values in list/combo fields. If this array     * is <CODE>null</CODE> then the choice values will also be used     * as the export values.     * @param choiceExports the export values in list/combo fields     */    public void setChoiceExports(String[] choiceExports) {        this.choiceExports = choiceExports;    }        /** Gets the zero based index of the selected item.     * @return the zero based index of the selected item     */    public int getChoiceSelection() {        return this.choiceSelection;    }        /** Sets the zero based index of the selected item.     * @param choiceSelection the zero based index of the selected item     */    public void setChoiceSelection(int choiceSelection) {        this.choiceSelection = choiceSelection;    }        int getTopFirst() {        return topFirst;    }        /**     * Sets extra margins in text fields to better mimic the Acrobat layout.     * @param extraMarginLeft the extra margin left     * @param extraMarginTop the extra margin top     */        public void setExtraMargin(float extraMarginLeft, float extraMarginTop) {        this.extraMarginLeft = extraMarginLeft;        this.extraMarginTop = extraMarginTop;    }    /**     * Holds value of property substitutionFonts.     */    private ArrayList substitutionFonts;    /**     * Gets the list of substitution fonts. The list is composed of <CODE>BaseFont</CODE> and can be <CODE>null</CODE>. The fonts in this list will be used if the original     * font doesn't contain the needed glyphs.     * @return the list     */    public ArrayList getSubstitutionFonts() {        return this.substitutionFonts;    }    /**     * Sets a list of substitution fonts. The list is composed of <CODE>BaseFont</CODE> and can also be <CODE>null</CODE>. The fonts in this list will be used if the original     * font doesn't contain the needed glyphs.     * @param substitutionFonts the list     */    public void setSubstitutionFonts(ArrayList substitutionFonts) {        this.substitutionFonts = substitutionFonts;    }    /**     * Holds value of property extensionFont.     */    private BaseFont extensionFont;    /**     * Gets the extensionFont. This font will be searched before the     * substitution fonts. It may be <code>null</code>.     * @return the extensionFont     */    public BaseFont getExtensionFont() {        return this.extensionFont;    }    /**     * Sets the extensionFont. This font will be searched before the     * substitution fonts. It may be <code>null</code>.     * @param extensionFont New value of property extensionFont.     */    public void setExtensionFont(BaseFont extensionFont) {        this.extensionFont = extensionFont;    }}

⌨️ 快捷键说明

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