1 47 48 package com.lowagie.text.pdf; 49 50 import java.awt.Color ; 51 import java.io.IOException ; 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.Iterator ; 55 56 import com.lowagie.text.DocumentException; 57 import com.lowagie.text.Element; 58 import com.lowagie.text.Rectangle; 59 60 63 public abstract class BaseField { 64 65 66 public static final float BORDER_WIDTH_THIN = 1; 67 68 public static final float BORDER_WIDTH_MEDIUM = 2; 69 70 public static final float BORDER_WIDTH_THICK = 3; 71 72 public static final int VISIBLE = 0; 73 74 public static final int HIDDEN = 1; 75 76 public static final int VISIBLE_BUT_DOES_NOT_PRINT = 2; 77 78 public static final int HIDDEN_BUT_PRINTABLE = 3; 79 80 public static final int READ_ONLY = PdfFormField.FF_READ_ONLY; 81 84 public static final int REQUIRED = PdfFormField.FF_REQUIRED; 85 88 public static final int MULTILINE = PdfFormField.FF_MULTILINE; 89 94 public static final int DO_NOT_SCROLL = PdfFormField.FF_DONOTSCROLL; 95 98 public static final int PASSWORD = PdfFormField.FF_PASSWORD; 99 102 public static final int FILE_SELECTION = PdfFormField.FF_FILESELECT; 103 107 public static final int DO_NOT_SPELL_CHECK = PdfFormField.FF_DONOTSPELLCHECK; 108 112 public static final int EDIT = PdfFormField.FF_EDIT; 113 114 117 public static final int COMB = PdfFormField.FF_COMB; 118 119 protected float borderWidth = BORDER_WIDTH_THIN; 120 protected int borderStyle = PdfBorderDictionary.STYLE_SOLID; 121 protected Color borderColor; 122 protected Color backgroundColor; 123 protected Color textColor; 124 protected BaseFont font; 125 protected float fontSize = 0; 126 protected int alignment = Element.ALIGN_LEFT; 127 protected PdfWriter writer; 128 protected String text; 129 protected Rectangle box; 130 131 132 protected int rotation = 0; 133 134 135 protected int visibility; 136 137 138 protected String fieldName; 139 140 141 protected int options; 142 143 144 protected int maxCharacterLength; 145 146 private final static HashMap fieldKeys = new HashMap (); 147 148 static { 149 fieldKeys.putAll(PdfCopyFieldsImp.fieldKeys); 150 fieldKeys.put(PdfName.T, new Integer (1)); 151 } 152 158 public BaseField(PdfWriter writer, Rectangle box, String fieldName) { 159 this.writer = writer; 160 setBox(box); 161 this.fieldName = fieldName; 162 } 163 164 protected BaseFont getRealFont() throws IOException , DocumentException { 165 if (font == null) 166 return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false); 167 else 168 return font; 169 } 170 171 protected PdfAppearance getBorderAppearance() { 172 PdfAppearance app = PdfAppearance.createAppearance(writer, box.getWidth(), box.getHeight()); 173 switch (rotation) { 174 case 90: 175 app.setMatrix(0, 1, -1, 0, box.getHeight(), 0); 176 break; 177 case 180: 178 app.setMatrix(-1, 0, 0, -1, box.getWidth(), box.getHeight()); 179 break; 180 case 270: 181 app.setMatrix(0, -1, 1, 0, 0, box.getWidth()); 182 break; 183 } 184 if (backgroundColor != null) { 186 app.setColorFill(backgroundColor); 187 app.rectangle(0, 0, box.getWidth(), box.getHeight()); 188 app.fill(); 189 } 190 if (borderStyle == PdfBorderDictionary.STYLE_UNDERLINE) { 192 if (borderWidth != 0 && borderColor != null) { 193 app.setColorStroke(borderColor); 194 app.setLineWidth(borderWidth); 195 app.moveTo(0, borderWidth / 2); 196 app.lineTo(box.getWidth(), borderWidth / 2); 197 app.stroke(); 198 } 199 } 200 else if (borderStyle == PdfBorderDictionary.STYLE_BEVELED) { 201 if (borderWidth != 0 && borderColor != null) { 202 app.setColorStroke(borderColor); 203 app.setLineWidth(borderWidth); 204 app.rectangle(borderWidth / 2, borderWidth / 2, box.getWidth() - borderWidth, box.getHeight() - borderWidth); 205 app.stroke(); 206 } 207 Color actual = backgroundColor; 209 if (actual == null) 210 actual = Color.white; 211 app.setGrayFill(1); 212 drawTopFrame(app); 213 app.setColorFill(actual.darker()); 214 drawBottomFrame(app); 215 } 216 else if (borderStyle == PdfBorderDictionary.STYLE_INSET) { 217 if (borderWidth != 0 && borderColor != null) { 218 app.setColorStroke(borderColor); 219 app.setLineWidth(borderWidth); 220 app.rectangle(borderWidth / 2, borderWidth / 2, box.getWidth() - borderWidth, box.getHeight() - borderWidth); 221 app.stroke(); 222 } 223 app.setGrayFill(0.5f); 225 drawTopFrame(app); 226 app.setGrayFill(0.75f); 227 drawBottomFrame(app); 228 } 229 else { 230 if (borderWidth != 0 && borderColor != null) { 231 if (borderStyle == PdfBorderDictionary.STYLE_DASHED) 232 app.setLineDash(3, 0); 233 app.setColorStroke(borderColor); 234 app.setLineWidth(borderWidth); 235 app.rectangle(borderWidth / 2, borderWidth / 2, box.getWidth() - borderWidth, box.getHeight() - borderWidth); 236 app.stroke(); 237 if ((options & COMB) != 0 && maxCharacterLength > 1) { 238 float step = box.getWidth() / maxCharacterLength; 239 float yb = borderWidth / 2; 240 float yt = box.getHeight() - borderWidth / 2; 241 for (int k = 1; k < maxCharacterLength; ++k) { 242 float x = step * k; 243 app.moveTo(x, yb); 244 app.lineTo(x, yt); 245 } 246 app.stroke(); 247 } 248 } 249 } 250 return app; 251 } 252 253 protected static ArrayList getHardBreaks(String text) { 254 ArrayList arr = new ArrayList (); 255 char cs[] = text.toCharArray(); 256 int len = cs.length; 257 StringBuffer buf = new StringBuffer (); 258 for (int k = 0; k < len; ++k) { 259 char c = cs[k]; 260 if (c == '\r') { 261 if (k + 1 < len && cs[k + 1] == '\n') 262 ++k; 263 arr.add(buf.toString()); 264 buf = new StringBuffer (); 265 } 266 else if (c == '\n') { 267 arr.add(buf.toString()); 268 buf = new StringBuffer (); 269 } 270 else 271 buf.append(c); 272 } 273 arr.add(buf.toString()); 274 return arr; 275 } 276 277 protected static void trimRight(StringBuffer buf) { 278 int len = buf.length(); 279 while (true) { 280 if (len == 0) 281 return; 282 if (buf.charAt(--len) != ' ') 283 return; 284 buf.setLength(len); 285 } 286 } 287 288 protected static ArrayList breakLines(ArrayList breaks, BaseFont font, float fontSize, float width) { 289 ArrayList lines = new ArrayList (); 290 StringBuffer buf = new StringBuffer (); 291 for (int ck = 0; ck < breaks.size(); ++ck) { 292 buf.setLength(0); 293 float w = 0; 294 char cs[] = ((String )breaks.get(ck)).toCharArray(); 295 int len = cs.length; 296 int state = 0; 298 int lastspace = -1; 299 char c = 0; 300 int refk = 0; 301 for (int k = 0; k < len; ++k) { 302 c = cs[k]; 303 switch (state) { 304 case 0: 305 w += font.getWidthPoint(c, fontSize); 306 buf.append(c); 307 if (w > width) { 308 w = 0; 309 if (buf.length() > 1) { 310 --k; 311 buf.setLength(buf.length() - 1); 312 } 313 lines.add(buf.toString()); 314 buf.setLength(0); 315 refk = k; 316 if (c == ' ') 317 state = 2; 318 else 319 state = 1; 320 } 321 else { 322 if (c != ' ') 323 state = 1; 324 } 325 break; 326 case 1: 327 w += font.getWidthPoint(c, fontSize); 328 buf.append(c); 329 if (c == ' ') 330 lastspace = k; 331 if (w > width) { 332 w = 0; 333 if (lastspace >= 0) { 334 k = lastspace; 335 buf.setLength(lastspace - refk); 336 trimRight(buf); 337 lines.add(buf.toString()); 338 buf.setLength(0); 339 refk = k; 340 lastspace = -1; 341 state = 2; 342 } 343 else { 344 if (buf.length() > 1) { 345 --k; 346 buf.setLength(buf.length() - 1); 347 } 348 lines.add(buf.toString()); 349 buf.setLength(0); 350 refk = k; 351 if (c == ' ') 352 state = 2; 353 } 354 } 355 break; 356 case 2: 357 if (c != ' ') { 358 w = 0; 359 --k; 360 state = 1; 361 } 362 break; 363 } 364 } 365 trimRight(buf); 366 lines.add(buf.toString()); 367 } 368 return lines; 369 } 370 371 private void drawTopFrame(PdfAppearance app) { 372 app.moveTo(borderWidth, borderWidth); 373 app.lineTo(borderWidth, box.getHeight() - borderWidth); 374 app.lineTo(box.getWidth() - borderWidth, box.getHeight() - borderWidth); 375 app.lineTo(box.getWidth() - 2 * borderWidth, box.getHeight() - 2 * borderWidth); 376 app.lineTo(2 * borderWidth, box.getHeight() - 2 * borderWidth); 377 app.lineTo(2 * borderWidth, 2 * borderWidth); 378 app.lineTo(borderWidth, borderWidth); 379 app.fill(); 380 } 381 382 private void drawBottomFrame(PdfAppearance app) { 383 app.moveTo(borderWidth, borderWidth); 384 app.lineTo(box.getWidth() - borderWidth, borderWidth); 385 app.lineTo(box.getWidth() - borderWidth, box.getHeight() - borderWidth); 386 app.lineTo(box.getWidth() - 2 * borderWidth, box.getHeight() - 2 * borderWidth); 387 app.lineTo(box.getWidth() - 2 * borderWidth, 2 * borderWidth); 388 app.lineTo(2 * borderWidth, 2 * borderWidth); 389 app.lineTo(borderWidth, borderWidth); 390 app.fill(); 391 } 392 395 public float getBorderWidth() { 396 return this.borderWidth; 397 } 398 399 403 public void setBorderWidth(float borderWidth) { 404 this.borderWidth = borderWidth; 405 } 406 407 410 public int getBorderStyle() { 411 return this.borderStyle; 412 } 413 414 420 public void setBorderStyle(int borderStyle) { 421 this.borderStyle = borderStyle; 422 } 423 424 427 public Color getBorderColor() { 428 return this.borderColor; 429 } 430 431 435 public void setBorderColor(Color borderColor) { 436 this.borderColor = borderColor; 437 } 438 439 442 public Color getBackgroundColor() { 443 return this.backgroundColor; 444 } 445 446 450 public void setBackgroundColor(Color backgroundColor) { 451 this.backgroundColor = backgroundColor; 452 } 453 454 457 public Color getTextColor() { 458 return this.textColor; 459 } 460 461 465 public void setTextColor(Color textColor) { 466 this.textColor = textColor; 467 } 468 469 472 public BaseFont getFont() { 473 return this.font; 474 } 475 476 480 public void setFont(BaseFont font) { 481 this.font = font; 482 } 483 484 487 public float getFontSize() { 488 return this.fontSize; 489 } 490 491 495 public void setFontSize(float fontSize) { 496 this.fontSize = fontSize; 497 } 498 499 502 public int getAlignment() { 503 return this.alignment; 504 } 505 506 510 public void setAlignment(int alignment) { 511 this.alignment = alignment; 512 } 513 514 517 public String getText() { 518 return this.text; 519 } 520 521 524 public void setText(String text) { 525 this.text = text; 526 } 527 528 531 public Rectangle getBox() { 532 return this.box; 533 } 534 535 538 public void setBox(Rectangle box) { 539 if (box == null) { 540 this.box = null; 541 } 542 else { 543 this.box = new Rectangle(box); 544 this.box.normalize(); 545 } 546 } 547 548 551 public int getRotation() { 552 return this.rotation; 553 } 554 555 559 public void setRotation(int rotation) { 560 if (rotation % 90 != 0) 561 throw new IllegalArgumentException ("Rotation must be a multiple of 90."); 562 rotation %= 360; 563 if (rotation < 0) 564 rotation += 360; 565 this.rotation = rotation; 566 } 567 568 572 public void setRotationFromPage(Rectangle page) { 573 setRotation(page.getRotation()); 574 } 575 576 579 public int getVisibility() { 580 return this.visibility; 581 } 582 583 588 public void setVisibility(int visibility) { 589 this.visibility = visibility; 590 } 591 592 595 public String getFieldName() { 596 return this.fieldName; 597 } 598 599 603 public void setFieldName(String fieldName) { 604 this.fieldName = fieldName; 605 } 606 607 610 public int getOptions() { 611 return this.options; 612 } 613 614 621 public void setOptions(int options) { 622 this.options = options; 623 } 624 625 628 public int getMaxCharacterLength() { 629 return this.maxCharacterLength; 630 } 631 632 636 public void setMaxCharacterLength(int maxCharacterLength) { 637 this.maxCharacterLength = maxCharacterLength; 638 } 639 640 644 public PdfWriter getWriter() { 645 return writer; 646 } 647 648 652 public void setWriter(PdfWriter writer) { 653 this.writer = writer; 654 } 655 656 662 public static void moveFields(PdfDictionary from, PdfDictionary to) { 663 for (Iterator i = from.getKeys().iterator(); i.hasNext();) { 664 PdfName key = (PdfName)i.next(); 665 if (fieldKeys.containsKey(key)) { 666 if (to != null) 667 to.put(key, from.get(key)); 668 i.remove(); 669 } 670 } 671 } 672 } 673 | Popular Tags |