| Code with Finding: |
class TextField {
/**
* Get the <code>PdfAppearance</code> of a list field
* @throws IOException on error
* @throws DocumentException on error
* @return A <code>PdfAppearance</code>
*/
PdfAppearance getListAppearance() throws IOException, DocumentException {
PdfAppearance app = getBorderAppearance();
if (choices == null || choices.length == 0) {
return app;
}
app.beginVariableText();
int topChoice = getTopChoice();
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;
float offsetX = borderWidth;
if (borderExtra) {
h -= borderWidth * 2;
offsetX *= 2;
}
float leading = ufont.getFontDescriptor(BaseFont.BBOXURY, usize) - ufont.getFontDescriptor(BaseFont.BBOXLLY, usize);
int maxFit = (int)(h / leading) + 1;
int first = 0;
int last = 0;
first = topChoice;
last = first + maxFit;
if (last > choices.length)
last = choices.length;
topFirst = first;
app.saveState();
app.rectangle(offsetX, offsetX, box.getWidth() - 2 * offsetX, box.getHeight() - 2 * offsetX);
app.clip();
app.newPath();
BaseColor fcolor = textColor == null ? GrayColor.GRAYBLACK : textColor;
// background boxes for selected value[s]
app.setColorFill(new BaseColor(10, 36, 106));
for (int curVal = 0; curVal < choiceSelections.size(); ++curVal) {
int curChoice = (choiceSelections.get( curVal )).intValue();
// only draw selections within our display range... not strictly necessary with
// that clipping rect from above, but it certainly doesn't hurt either
if (curChoice >= first && curChoice <= last) {
app.rectangle(offsetX, offsetX + h - (curChoice - 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);
// highlight selected values against their (presumably) darker background
BaseColor textCol = choiceSelections.contains( Integer.valueOf( idx )) ? GrayColor.GRAYWHITE : fcolor;
Phrase phrase = composePhrase(ptext, ufont, textCol, usize);
ColumnText.showTextAligned(app, Element.ALIGN_LEFT, phrase, xp, yp, 0, rtl, 0);
}
app.restoreState();
app.endVariableText();
return app;
}
}
class TextField {
protected PdfFormField getChoiceField(boolean isList) throws IOException, DocumentException {
options &= ~MULTILINE & ~COMB;
String uchoices[] = choices;
if (uchoices == null)
uchoices = new String[0];
int topChoice = getTopChoice();
if (text == null)
text = ""; //fixed by Kazuya Ujihara (ujihara.jp)
if (topChoice >= 0)
text = uchoices[topChoice];
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) {
if (choiceSelections.size() < 2) {
field.setValueAsString(mix[topChoice][0]);
field.setDefaultValueAsString(mix[topChoice][0]);
} else {
writeMultipleValues( field, mix);
}
} else {
if (choiceSelections.size() < 2) {
field.setValueAsString(text);
field.setDefaultValueAsString(text);
} else {
writeMultipleValues( field, null );
}
}
}
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);
if ((options & MULTISELECT) != 0) {
field.setFieldFlags( PdfFormField.FF_MULTISELECT );
}
}
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;
}
}
|