KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > BaseField


1 /*
2  * Copyright 2005 by Paulo Soares.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47
48 package com.lowagie.text.pdf;
49
50 import java.awt.Color JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.lowagie.text.DocumentException;
57 import com.lowagie.text.Element;
58 import com.lowagie.text.Rectangle;
59
60 /** Common field variables.
61  * @author Paulo Soares (psoares@consiste.pt)
62  */

63 public abstract class BaseField {
64     
65     /** A thin border with 1 point width. */
66     public static final float BORDER_WIDTH_THIN = 1;
67     /** A medium border with 2 point width. */
68     public static final float BORDER_WIDTH_MEDIUM = 2;
69     /** A thick border with 3 point width. */
70     public static final float BORDER_WIDTH_THICK = 3;
71     /** The field is visible. */
72     public static final int VISIBLE = 0;
73     /** The field is hidden. */
74     public static final int HIDDEN = 1;
75     /** The field is visible but does not print. */
76     public static final int VISIBLE_BUT_DOES_NOT_PRINT = 2;
77     /** The field is hidden but is printable. */
78     public static final int HIDDEN_BUT_PRINTABLE = 3;
79     /** The user may not change the value of the field. */
80     public static final int READ_ONLY = PdfFormField.FF_READ_ONLY;
81     /** The field must have a value at the time it is exported by a submit-form
82      * action.
83      */

84     public static final int REQUIRED = PdfFormField.FF_REQUIRED;
85     /** The field may contain multiple lines of text.
86      * This flag is only meaningful with text fields.
87      */

88     public static final int MULTILINE = PdfFormField.FF_MULTILINE;
89     /** The field will not scroll (horizontally for single-line
90      * fields, vertically for multiple-line fields) to accommodate more text
91      * than will fit within its annotation rectangle. Once the field is full, no
92      * further text will be accepted.
93      */

94     public static final int DO_NOT_SCROLL = PdfFormField.FF_DONOTSCROLL;
95     /** The field is intended for entering a secure password that should
96      * not be echoed visibly to the screen.
97      */

98     public static final int PASSWORD = PdfFormField.FF_PASSWORD;
99     /** The text entered in the field represents the pathname of
100      * a file whose contents are to be submitted as the value of the field.
101      */

102     public static final int FILE_SELECTION = PdfFormField.FF_FILESELECT;
103     /** The text entered in the field will not be spell-checked.
104      * This flag is meaningful only in text fields and in combo
105      * fields with the <CODE>EDIT</CODE> flag set.
106      */

107     public static final int DO_NOT_SPELL_CHECK = PdfFormField.FF_DONOTSPELLCHECK;
108     /** If set the combo box includes an editable text box as well as a drop list; if
109      * clear, it includes only a drop list.
110      * This flag is only meaningful with combo fields.
111      */

112     public static final int EDIT = PdfFormField.FF_EDIT;
113
114     /**
115      * combo box flag.
116      */

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 JavaDoc borderColor;
122     protected Color JavaDoc backgroundColor;
123     protected Color JavaDoc textColor;
124     protected BaseFont font;
125     protected float fontSize = 0;
126     protected int alignment = Element.ALIGN_LEFT;
127     protected PdfWriter writer;
128     protected String JavaDoc text;
129     protected Rectangle box;
130     
131     /** Holds value of property rotation. */
132     protected int rotation = 0;
133     
134     /** Holds value of property visibility. */
135     protected int visibility;
136     
137     /** Holds value of property fieldName. */
138     protected String JavaDoc fieldName;
139     
140     /** Holds value of property options. */
141     protected int options;
142     
143     /** Holds value of property maxCharacterLength. */
144     protected int maxCharacterLength;
145     
146     private final static HashMap JavaDoc fieldKeys = new HashMap JavaDoc();
147  
148     static {
149         fieldKeys.putAll(PdfCopyFieldsImp.fieldKeys);
150         fieldKeys.put(PdfName.T, new Integer JavaDoc(1));
151     }
152     /** Creates a new <CODE>TextField</CODE>.
153      * @param writer the document <CODE>PdfWriter</CODE>
154      * @param box the field location and dimensions
155      * @param fieldName the field name. If <CODE>null</CODE> only the widget keys
156      * will be included in the field allowing it to be used as a kid field.
157      */

158     public BaseField(PdfWriter writer, Rectangle box, String JavaDoc fieldName) {
159         this.writer = writer;
160         setBox(box);
161         this.fieldName = fieldName;
162     }
163     
164     protected BaseFont getRealFont() throws IOException JavaDoc, 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         // background
185
if (backgroundColor != null) {
186             app.setColorFill(backgroundColor);
187             app.rectangle(0, 0, box.getWidth(), box.getHeight());
188             app.fill();
189         }
190         // border
191
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             // beveled
208
Color JavaDoc 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             // inset
224
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 JavaDoc getHardBreaks(String JavaDoc text) {
254         ArrayList JavaDoc arr = new ArrayList JavaDoc();
255         char cs[] = text.toCharArray();
256         int len = cs.length;
257         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc();
265             }
266             else if (c == '\n') {
267                 arr.add(buf.toString());
268                 buf = new StringBuffer JavaDoc();
269             }
270             else
271                 buf.append(c);
272         }
273         arr.add(buf.toString());
274         return arr;
275     }
276     
277     protected static void trimRight(StringBuffer JavaDoc 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 JavaDoc breakLines(ArrayList JavaDoc breaks, BaseFont font, float fontSize, float width) {
289         ArrayList JavaDoc lines = new ArrayList JavaDoc();
290         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
291         for (int ck = 0; ck < breaks.size(); ++ck) {
292             buf.setLength(0);
293             float w = 0;
294             char cs[] = ((String JavaDoc)breaks.get(ck)).toCharArray();
295             int len = cs.length;
296             // 0 inline first, 1 inline, 2 spaces
297
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     /** Gets the border width in points.
393      * @return the border width in points
394      */

395     public float getBorderWidth() {
396         return this.borderWidth;
397     }
398     
399     /** Sets the border width in points. To eliminate the border
400      * set the border color to <CODE>null</CODE>.
401      * @param borderWidth the border width in points
402      */

403     public void setBorderWidth(float borderWidth) {
404         this.borderWidth = borderWidth;
405     }
406     
407     /** Gets the border style.
408      * @return the border style
409      */

410     public int getBorderStyle() {
411         return this.borderStyle;
412     }
413     
414     /** Sets the border style. The styles are found in <CODE>PdfBorderDictionary</CODE>
415      * and can be <CODE>STYLE_SOLID</CODE>, <CODE>STYLE_DASHED</CODE>,
416      * <CODE>STYLE_BEVELED</CODE>, <CODE>STYLE_INSET</CODE> and
417      * <CODE>STYLE_UNDERLINE</CODE>.
418      * @param borderStyle the border style
419      */

420     public void setBorderStyle(int borderStyle) {
421         this.borderStyle = borderStyle;
422     }
423     
424     /** Gets the border color.
425      * @return the border color
426      */

427     public Color JavaDoc getBorderColor() {
428         return this.borderColor;
429     }
430     
431     /** Sets the border color. Set to <CODE>null</CODE> to remove
432      * the border.
433      * @param borderColor the border color
434      */

435     public void setBorderColor(Color JavaDoc borderColor) {
436         this.borderColor = borderColor;
437     }
438     
439     /** Gets the background color.
440      * @return the background color
441      */

442     public Color JavaDoc getBackgroundColor() {
443         return this.backgroundColor;
444     }
445     
446     /** Sets the background color. Set to <CODE>null</CODE> for
447      * transparent background.
448      * @param backgroundColor the background color
449      */

450     public void setBackgroundColor(Color JavaDoc backgroundColor) {
451         this.backgroundColor = backgroundColor;
452     }
453     
454     /** Gets the text color.
455      * @return the text color
456      */

457     public Color JavaDoc getTextColor() {
458         return this.textColor;
459     }
460     
461     /** Sets the text color. If <CODE>null</CODE> the color used
462      * will be black.
463      * @param textColor the text color
464      */

465     public void setTextColor(Color JavaDoc textColor) {
466         this.textColor = textColor;
467     }
468     
469     /** Gets the text font.
470      * @return the text font
471      */

472     public BaseFont getFont() {
473         return this.font;
474     }
475     
476     /** Sets the text font. If <CODE>null</CODE> then Helvetica
477      * will be used.
478      * @param font the text font
479      */

480     public void setFont(BaseFont font) {
481         this.font = font;
482     }
483     
484     /** Gets the font size.
485      * @return the font size
486      */

487     public float getFontSize() {
488         return this.fontSize;
489     }
490     
491     /** Sets the font size. If 0 then auto-sizing will be used but
492      * only for text fields.
493      * @param fontSize the font size
494      */

495     public void setFontSize(float fontSize) {
496         this.fontSize = fontSize;
497     }
498     
499     /** Gets the text horizontal alignment.
500      * @return the text horizontal alignment
501      */

502     public int getAlignment() {
503         return this.alignment;
504     }
505     
506     /** Sets the text horizontal alignment. It can be <CODE>Element.ALIGN_LEFT</CODE>,
507      * <CODE>Element.ALIGN_CENTER</CODE> and <CODE>Element.ALIGN_RIGHT</CODE>.
508      * @param alignment the text horizontal alignment
509      */

510     public void setAlignment(int alignment) {
511         this.alignment = alignment;
512     }
513     
514     /** Gets the text.
515      * @return the text
516      */

517     public String JavaDoc getText() {
518         return this.text;
519     }
520     
521     /** Sets the text for text fields.
522      * @param text the text
523      */

524     public void setText(String JavaDoc text) {
525         this.text = text;
526     }
527     
528     /** Gets the field dimension and position.
529      * @return the field dimension and position
530      */

531     public Rectangle getBox() {
532         return this.box;
533     }
534     
535     /** Sets the field dimension and position.
536      * @param box the field dimension and position
537      */

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     /** Gets the field rotation.
549      * @return the field rotation
550      */

551     public int getRotation() {
552         return this.rotation;
553     }
554     
555     /** Sets the field rotation. This value should be the same as
556      * the page rotation where the field will be shown.
557      * @param rotation the field rotation
558      */

559     public void setRotation(int rotation) {
560         if (rotation % 90 != 0)
561             throw new IllegalArgumentException JavaDoc("Rotation must be a multiple of 90.");
562         rotation %= 360;
563         if (rotation < 0)
564             rotation += 360;
565         this.rotation = rotation;
566     }
567     
568     /** Convenience method to set the field rotation the same as the
569      * page rotation.
570      * @param page the page
571      */

572     public void setRotationFromPage(Rectangle page) {
573         setRotation(page.getRotation());
574     }
575     
576     /** Gets the field visibility flag.
577      * @return the field visibility flag
578      */

579     public int getVisibility() {
580         return this.visibility;
581     }
582     
583     /** Sets the field visibility flag. This flags can be one of
584      * <CODE>VISIBLE</CODE>, <CODE>HIDDEN</CODE>, <CODE>VISIBLE_BUT_DOES_NOT_PRINT</CODE>
585      * and <CODE>HIDDEN_BUT_PRINTABLE</CODE>.
586      * @param visibility field visibility flag
587      */

588     public void setVisibility(int visibility) {
589         this.visibility = visibility;
590     }
591     
592     /** Gets the field name.
593      * @return the field name
594      */

595     public String JavaDoc getFieldName() {
596         return this.fieldName;
597     }
598     
599     /** Sets the field name.
600      * @param fieldName the field name. If <CODE>null</CODE> only the widget keys
601      * will be included in the field allowing it to be used as a kid field.
602      */

603     public void setFieldName(String JavaDoc fieldName) {
604         this.fieldName = fieldName;
605     }
606     
607     /** Gets the option flags.
608      * @return the option flags
609      */

610     public int getOptions() {
611         return this.options;
612     }
613     
614     /** Sets the option flags. The option flags can be a combination by oring of
615      * <CODE>READ_ONLY</CODE>, <CODE>REQUIRED</CODE>,
616      * <CODE>MULTILINE</CODE>, <CODE>DO_NOT_SCROLL</CODE>,
617      * <CODE>PASSWORD</CODE>, <CODE>FILE_SELECTION</CODE>,
618      * <CODE>DO_NOT_SPELL_CHECK</CODE> and <CODE>EDIT</CODE>.
619      * @param options the option flags
620      */

621     public void setOptions(int options) {
622         this.options = options;
623     }
624     
625     /** Gets the maximum length of the field's text, in characters.
626      * @return the maximum length of the field's text, in characters.
627      */

628     public int getMaxCharacterLength() {
629         return this.maxCharacterLength;
630     }
631     
632     /** Sets the maximum length of the field's text, in characters.
633      * It is only meaningful for text fields.
634      * @param maxCharacterLength the maximum length of the field's text, in characters
635      */

636     public void setMaxCharacterLength(int maxCharacterLength) {
637         this.maxCharacterLength = maxCharacterLength;
638     }
639     
640     /**
641      * Getter for property writer.
642      * @return Value of property writer.
643      */

644     public PdfWriter getWriter() {
645         return writer;
646     }
647     
648     /**
649      * Setter for property writer.
650      * @param writer New value of property writer.
651      */

652     public void setWriter(PdfWriter writer) {
653         this.writer = writer;
654     }
655     
656     /**
657      * Moves the field keys from <CODE>from</CODE> to <CODE>to</CODE>. The moved keys
658      * are removed from <CODE>from</CODE>.
659      * @param from the source
660      * @param to the destination. It may be <CODE>null</CODE>
661      */

662     public static void moveFields(PdfDictionary from, PdfDictionary to) {
663         for (Iterator JavaDoc 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