1 47 package com.lowagie.text.pdf; 48 49 import java.io.IOException ; 50 51 import com.lowagie.text.DocumentException; 52 import com.lowagie.text.ExceptionConverter; 53 import com.lowagie.text.Rectangle; 54 55 98 public class RadioCheckField extends BaseField { 99 100 101 public static final int TYPE_CHECK = 1; 102 103 public static final int TYPE_CIRCLE = 2; 104 105 public static final int TYPE_CROSS = 3; 106 107 public static final int TYPE_DIAMOND = 4; 108 109 public static final int TYPE_SQUARE = 5; 110 111 public static final int TYPE_STAR = 6; 112 113 private static String typeChars[] = {"4", "l", "8", "u", "n", "H"}; 114 115 118 private int checkType; 119 120 123 private String onValue; 124 125 128 private boolean checked; 129 130 137 public RadioCheckField(PdfWriter writer, Rectangle box, String fieldName, String onValue) { 138 super(writer, box, fieldName); 139 setOnValue(onValue); 140 setCheckType(TYPE_CIRCLE); 141 } 142 143 147 public int getCheckType() { 148 return this.checkType; 149 } 150 151 161 public void setCheckType(int checkType) { 162 if (checkType < TYPE_CHECK || checkType > TYPE_STAR) 163 checkType = TYPE_CIRCLE; 164 this.checkType = checkType; 165 setText(typeChars[checkType - 1]); 166 try { 167 setFont(BaseFont.createFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, false)); 168 } 169 catch (Exception e) { 170 throw new ExceptionConverter(e); 171 } 172 } 173 174 178 public String getOnValue() { 179 return this.onValue; 180 } 181 182 186 public void setOnValue(String onValue) { 187 this.onValue = onValue; 188 } 189 190 194 public boolean isChecked() { 195 return this.checked; 196 } 197 198 203 public void setChecked(boolean checked) { 204 this.checked = checked; 205 } 206 207 217 public PdfAppearance getAppearance(boolean isRadio, boolean on) throws IOException , DocumentException { 218 if (isRadio && checkType == TYPE_CIRCLE) 219 return getAppearanceRadioCircle(on); 220 PdfAppearance app = getBorderAppearance(); 221 if (!on) 222 return app; 223 BaseFont ufont = getRealFont(); 224 boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; 225 float h = box.getHeight() - borderWidth * 2; 226 float bw2 = borderWidth; 227 if (borderExtra) { 228 h -= borderWidth * 2; 229 bw2 *= 2; 230 } 231 float offsetX = (borderExtra ? 2 * borderWidth : borderWidth); 232 offsetX = Math.max(offsetX, 1); 233 float offX = Math.min(bw2, offsetX); 234 float wt = box.getWidth() - 2 * offX; 235 float ht = box.getHeight() - 2 * offX; 236 float fsize = fontSize; 237 if (fsize == 0) { 238 float bw = ufont.getWidthPoint(text, 1); 239 if (bw == 0) 240 fsize = 12; 241 else 242 fsize = wt / bw; 243 float nfsize = h / (ufont.getFontDescriptor(BaseFont.ASCENT, 1)); 244 fsize = Math.min(fsize, nfsize); 245 } 246 app.saveState(); 247 app.rectangle(offX, offX, wt, ht); 248 app.clip(); 249 app.newPath(); 250 if (textColor == null) 251 app.resetGrayFill(); 252 else 253 app.setColorFill(textColor); 254 app.beginText(); 255 app.setFontAndSize(ufont, fsize); 256 app.setTextMatrix((box.getWidth() - ufont.getWidthPoint(text, fsize)) / 2, 257 (box.getHeight() - ufont.getAscentPoint(text, fsize)) / 2); 258 app.showText(text); 259 app.endText(); 260 app.restoreState(); 261 return app; 262 } 263 264 270 public PdfAppearance getAppearanceRadioCircle(boolean on) { 271 PdfAppearance app = PdfAppearance.createAppearance(writer, box.getWidth(), box.getHeight()); 272 switch (rotation) { 273 case 90: 274 app.setMatrix(0, 1, -1, 0, box.getHeight(), 0); 275 break; 276 case 180: 277 app.setMatrix(-1, 0, 0, -1, box.getWidth(), box.getHeight()); 278 break; 279 case 270: 280 app.setMatrix(0, -1, 1, 0, 0, box.getWidth()); 281 break; 282 } 283 Rectangle box = new Rectangle(app.getBoundingBox()); 284 float cx = box.getWidth() / 2; 285 float cy = box.getHeight() / 2; 286 float r = (Math.min(box.getWidth(), box.getHeight()) - borderWidth) / 2; 287 if (r <= 0) 288 return app; 289 if (backgroundColor != null) { 290 app.setColorFill(backgroundColor); 291 app.circle(cx, cy, r + borderWidth / 2); 292 app.fill(); 293 } 294 if (borderWidth > 0 && borderColor != null) { 295 app.setLineWidth(borderWidth); 296 app.setColorStroke(borderColor); 297 app.circle(cx, cy, r); 298 app.stroke(); 299 } 300 if (on) { 301 if (textColor == null) 302 app.resetGrayFill(); 303 else 304 app.setColorFill(textColor); 305 app.circle(cx, cy, r / 2); 306 app.fill(); 307 } 308 return app; 309 } 310 311 324 public PdfFormField getRadioGroup(boolean noToggleToOff, boolean radiosInUnison) { 325 PdfFormField field = PdfFormField.createRadioButton(writer, noToggleToOff); 326 if (radiosInUnison) 327 field.setFieldFlags(PdfFormField.FF_RADIOSINUNISON); 328 field.setFieldName(fieldName); 329 if ((options & READ_ONLY) != 0) 330 field.setFieldFlags(PdfFormField.FF_READ_ONLY); 331 if ((options & REQUIRED) != 0) 332 field.setFieldFlags(PdfFormField.FF_REQUIRED); 333 field.setValueAsName(checked ? onValue : "Off"); 334 return field; 335 } 336 337 344 public PdfFormField getRadioField() throws IOException , DocumentException { 345 return getField(true); 346 } 347 348 354 public PdfFormField getCheckField() throws IOException , DocumentException { 355 return getField(false); 356 } 357 358 366 protected PdfFormField getField(boolean isRadio) throws IOException , DocumentException { 367 PdfFormField field = null; 368 if (isRadio) 369 field = PdfFormField.createEmpty(writer); 370 else 371 field = PdfFormField.createCheckBox(writer); 372 field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT); 373 if (!isRadio) { 374 field.setFieldName(fieldName); 375 if ((options & READ_ONLY) != 0) 376 field.setFieldFlags(PdfFormField.FF_READ_ONLY); 377 if ((options & REQUIRED) != 0) 378 field.setFieldFlags(PdfFormField.FF_REQUIRED); 379 field.setValueAsName(checked ? onValue : "Off"); 380 } 381 if (text != null) 382 field.setMKNormalCaption(text); 383 if (rotation != 0) 384 field.setMKRotation(rotation); 385 field.setBorderStyle(new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3))); 386 PdfAppearance tpon = getAppearance(isRadio, true); 387 PdfAppearance tpoff = getAppearance(isRadio, false); 388 field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, onValue, tpon); 389 field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpoff); 390 field.setAppearanceState(checked ? onValue : "Off"); 391 PdfAppearance da = (PdfAppearance)tpon.getDuplicate(); 392 da.setFontAndSize(getRealFont(), fontSize); 393 if (textColor == null) 394 da.setGrayFill(0); 395 else 396 da.setColorFill(textColor); 397 field.setDefaultAppearanceString(da); 398 if (borderColor != null) 399 field.setMKBorderColor(borderColor); 400 if (backgroundColor != null) 401 field.setMKBackgroundColor(backgroundColor); 402 switch (visibility) { 403 case HIDDEN: 404 field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN); 405 break; 406 case VISIBLE_BUT_DOES_NOT_PRINT: 407 break; 408 case HIDDEN_BUT_PRINTABLE: 409 field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW); 410 break; 411 default: 412 field.setFlags(PdfAnnotation.FLAGS_PRINT); 413 break; 414 } 415 return field; 416 } 417 } | Popular Tags |