📄 textfield.java
字号:
/*
* Copyright 2003-2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
package com.lowagie.text.pdf;
import java.awt.Color;
import java.io.IOException;
import java.util.ArrayList;
import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
/** Supports text, combo and list fields generating the correct appearances.
* All the option in the Acrobat GUI are supported in an easy to use API.
* @author Paulo Soares (psoares@consiste.pt)
*/
public class TextField extends BaseField {
/** Holds value of property defaultText. */
private String defaultText;
/** Holds value of property choices. */
private String[] choices;
/** Holds value of property choiceExports. */
private String[] choiceExports;
/** Holds value of property choiceSelection. */
private int choiceSelection;
private int topFirst;
private float extraMarginLeft;
private float extraMarginTop;
/** Creates a new <CODE>TextField</CODE>.
* @param writer the document <CODE>PdfWriter</CODE>
* @param box the field location and dimensions
* @param fieldName the field name. If <CODE>null</CODE> only the widget keys
* will be included in the field allowing it to be used as a kid field.
*/
public TextField(PdfWriter writer, Rectangle box, String fieldName) {
super(writer, box, fieldName);
}
private static boolean checkRTL(String text) {
if (text == null || text.length() == 0)
return false;
char[] cc = text.toCharArray();
for (int k = 0; k < cc.length; ++k) {
int c = (int)cc[k];
if (c >= 0x590 && c < 0x0780)
return true;
}
return false;
}
private static void changeFontSize(Phrase p, float size) {
for (int k = 0; k < p.size(); ++k) {
((Chunk)p.get(k)).getFont().setSize(size);
}
}
private Phrase composePhrase(String text, BaseFont ufont, Color color, float fontSize) {
Phrase phrase = null;
if (extensionFont == null && (substitutionFonts == null || substitutionFonts.isEmpty()))
phrase = new Phrase(new Chunk(text, new Font(ufont, fontSize, 0, color)));
else {
FontSelector fs = new FontSelector();
fs.addFont(new Font(ufont, fontSize, 0, color));
if (extensionFont != null)
fs.addFont(new Font(extensionFont, fontSize, 0, color));
if (substitutionFonts != null) {
for (int k = 0; k < substitutionFonts.size(); ++k) {
fs.addFont(new Font((BaseFont)substitutionFonts.get(k), fontSize, 0, color));
}
}
phrase = fs.process(text);
}
return phrase;
}
private static String removeCRLF(String text) {
if (text.indexOf('\n') >= 0 || text.indexOf('\r') >= 0) {
char[] p = text.toCharArray();
StringBuffer sb = new StringBuffer(p.length);
for (int k = 0; k < p.length; ++k) {
char c = p[k];
if (c == '\n')
sb.append(' ');
else if (c == '\r') {
sb.append(' ');
if (k < p.length - 1 && p[k + 1] == '\n')
++k;
}
else
sb.append(c);
}
return sb.toString();
}
return text;
}
public PdfAppearance getAppearance() throws IOException, DocumentException {
PdfAppearance app = getBorderAppearance();
app.beginVariableText();
if (text == null || text.length() == 0) {
app.endVariableText();
return app;
}
BaseFont ufont = getRealFont();
boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
float h = box.getHeight() - borderWidth * 2;
float bw2 = borderWidth;
if (borderExtra) {
h -= borderWidth * 2;
bw2 *= 2;
}
h -= extraMarginTop;
float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
offsetX = Math.max(offsetX, 1);
float offX = Math.min(bw2, offsetX);
app.saveState();
app.rectangle(offX, offX, box.getWidth() - 2 * offX, box.getHeight() - 2 * offX);
app.clip();
app.newPath();
Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor;
String ptext = text; //fixed by Kazuya Ujihara (ujihara.jp)
if ((options & PASSWORD) != 0) {
char[] pchar = new char[text.length()];
for (int i = 0; i < text.length(); i++)
pchar[i] = '*';
ptext = new String(pchar);
}
int rtl = checkRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI;
if ((options & MULTILINE) == 0) {
ptext = removeCRLF(text);
}
Phrase phrase = composePhrase(ptext, ufont, fcolor, fontSize);
if ((options & MULTILINE) != 0) {
float usize = fontSize;
float width = box.getWidth() - 4 * offsetX - extraMarginLeft;
float factor = ufont.getFontDescriptor(BaseFont.BBOXURY, 1) - ufont.getFontDescriptor(BaseFont.BBOXLLY, 1);
ColumnText ct = new ColumnText(null);
if (usize == 0) {
usize = h / factor;
if (usize > 4) {
if (usize > 12)
usize = 12;
float step = Math.max((usize - 4) / 10, 0.2f);
ct.setSimpleColumn(0, -h, width, 0);
ct.setAlignment(alignment);
ct.setRunDirection(rtl);
for (; usize > 4; usize -= step) {
ct.setYLine(0);
changeFontSize(phrase, usize);
ct.setText(phrase);
ct.setLeading(factor * usize);
int status = ct.go(true);
if ((status & ColumnText.NO_MORE_COLUMN) == 0)
break;
}
}
if (usize < 4) {
usize = 4;
}
}
changeFontSize(phrase, usize);
ct.setCanvas(app);
float leading = usize * factor;
float offsetY = offsetX + h - ufont.getFontDescriptor(BaseFont.BBOXURY, usize);
ct.setSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.getWidth() - 2 * offsetX, offsetY + leading);
ct.setLeading(leading);
ct.setAlignment(alignment);
ct.setRunDirection(rtl);
ct.setText(phrase);
ct.go();
}
else {
float usize = fontSize;
if (usize == 0) {
float maxCalculatedSize = h / (ufont.getFontDescriptor(BaseFont.BBOXURX, 1) - ufont.getFontDescriptor(BaseFont.BBOXLLY, 1));
changeFontSize(phrase, 1);
float wd = ColumnText.getWidth(phrase, rtl, 0);
if (wd == 0)
usize = maxCalculatedSize;
else
usize = (box.getWidth() - extraMarginLeft - 4 * offsetX) / wd;
if (usize > maxCalculatedSize)
usize = maxCalculatedSize;
if (usize < 4)
usize = 4;
}
changeFontSize(phrase, usize);
float offsetY = offX + ((box.getHeight() - 2*offX) - ufont.getFontDescriptor(BaseFont.ASCENT, usize)) / 2;
if (offsetY < offX)
offsetY = offX;
if (offsetY - offX < -ufont.getFontDescriptor(BaseFont.DESCENT, usize)) {
float ny = -ufont.getFontDescriptor(BaseFont.DESCENT, usize) + offX;
float dy = box.getHeight() - offX - ufont.getFontDescriptor(BaseFont.ASCENT, usize);
offsetY = Math.min(ny, Math.max(offsetY, dy));
}
if ((options & COMB) != 0 && maxCharacterLength > 0) {
int textLen = Math.min(maxCharacterLength, ptext.length());
int position = 0;
if (alignment == Element.ALIGN_RIGHT) {
position = maxCharacterLength - textLen;
}
else if (alignment == Element.ALIGN_CENTER) {
position = (maxCharacterLength - textLen) / 2;
}
float step = (box.getWidth() - extraMarginLeft) / maxCharacterLength;
float start = step / 2 + position * step;
if (textColor == null)
app.setGrayFill(0);
else
app.setColorFill(textColor);
app.beginText();
for (int k = 0; k < phrase.size(); ++k) {
Chunk ck = (Chunk)phrase.get(k);
BaseFont bf = ck.getFont().getBaseFont();
app.setFontAndSize(bf, usize);
StringBuffer sb = ck.append("");
for (int j = 0; j < sb.length(); ++j) {
String c = sb.substring(j, j + 1);
float wd = bf.getWidthPoint(c, usize);
app.setTextMatrix(extraMarginLeft + start - wd / 2, offsetY - extraMarginTop);
app.showText(c);
start += step;
}
}
app.endText();
}
else {
if (alignment == Element.ALIGN_RIGHT) {
ColumnText.showTextAligned(app, Element.ALIGN_RIGHT, phrase, extraMarginLeft + box.getWidth() - 2 * offsetX, offsetY - extraMarginTop, 0, rtl, 0);
}
else if (alignment == Element.ALIGN_CENTER) {
ColumnText.showTextAligned(app, Element.ALIGN_CENTER, phrase, extraMarginLeft + box.getWidth() / 2, offsetY - extraMarginTop, 0, rtl, 0);
}
else
ColumnText.showTextAligned(app, Element.ALIGN_LEFT, phrase, extraMarginLeft + 2 * offsetX, offsetY - extraMarginTop, 0, rtl, 0);
}
}
app.restoreState();
app.endVariableText();
return app;
}
PdfAppearance getListAppearance() throws IOException, DocumentException {
PdfAppearance app = getBorderAppearance();
app.beginVariableText();
if (choices == null || choices.length == 0) {
app.endVariableText();
return app;
}
int topChoice = choiceSelection;
if (topChoice >= choices.length) {
topChoice = choices.length - 1;
}
if (topChoice < 0)
topChoice = 0;
BaseFont ufont = getRealFont();
float usize = fontSize;
if (usize == 0)
usize = 12;
boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET;
float h = box.getHeight() - borderWidth * 2;
if (borderExtra)
h -= borderWidth * 2;
float offsetX = (borderExtra ? 2 * borderWidth : borderWidth);
float leading = ufont.getFontDescriptor(BaseFont.BBOXURY, usize) - ufont.getFontDescriptor(BaseFont.BBOXLLY, usize);
int maxFit = (int)(h / leading) + 1;
int first = 0;
int last = 0;
last = topChoice + maxFit / 2 + 1;
first = last - maxFit;
if (first < 0) {
last += first;
first = 0;
}
// first = topChoice;
last = first + maxFit;
if (last > choices.length)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -