KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > faceless > pdf > FormText


1 // $Id: FormText.java,v 1.3 2003/10/06 12:40:06 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.io.*;
6 import java.util.*;
7 import java.awt.Color JavaDoc;
8
9 /**
10  * <p>
11  * A type of form element representing a Text Field. Text fields may be single
12  * or multi-line, or may represent a password or (in Acrobat 5.0) a filename.
13  * </p><p>
14  * Here's an example showing how to create a new single-line text field in a form.
15  * </p>
16  * <pre>
17  * Form form = pdf.getForm();
18  * FormText text = new FormText(pdf.getLastPage(), 100,100,300,120);
19  * form.addElement("AccountNumber", text);</pre>
20  * <p>And here's how to extract the value from an existing text field in a form</p>
21  * <pre>
22  * Form form = pdf.getForm();
23  * FormText text = (FormText)form.getElement("AccountNumber");
24  * String account = text.getValue();</pre>
25  * <p>
26  * To add validation to a field isn't difficult either - here's how to use
27  * two of the built-in JavaScript methods in Adobe Acrobat to limit the keypresses
28  * in the field to digits only, and to limit the final value to between 1930 and 1985.
29  * </p>
30  * <pre>
31  * FormText text = new FormText(pdf.getLastPage(), 100,100,300,120);
32  * PDFAnnotation annot = text.getAnnotations()[0];
33  * PDFAction onkey = PDFAction.formJavaScript("AFNumber_Keystroke(0,1,1,0,'',true);");
34  * PDFAction onchg = PDFAction.formJavaScript("AFRange_Validate(true,1930,true,1985);");
35  * annot.setEventAction(PDFAnnotation.EVENT_ONKEYPRESS, onkey);
36  * annot.setEventAction(PDFAnnotation.EVENT_ONCHANGE, onchg);
37  * </pre>
38  * @since 1.1.23
39  */

40 public final class FormText extends FormElement
41 {
42     FormText(org.faceless.pdf2.FormText b)
43     {
44         super(b);
45     }
46
47     /**
48      * Create a new FormText element. With this constructor the page annotation
49      * must be positioned explicitly by calling the {@link PDFAnnotation#setPage}
50      * and {@link PDFAnnotation#setRectangle} methods.
51      * @since 1.1.26
52      */

53     public FormText()
54     {
55     this(null,0,0,0,0);
56     }
57
58     /**
59      * Create a new FormText element and place it in the specified location
60      * @param page the page to place the field on
61      * @param x1 the left-most X co-ordinate of the field
62      * @param y1 the top-most Y co-ordinate of the field
63      * @param x2 the right-most X co-ordinate of the field
64      * @param x2 the bottom-most Y co-ordinate of the field
65      */

66     public FormText(PDFPage page, float x1, float y1, float x2, float y2)
67     {
68     super(new org.faceless.pdf2.FormText(page==null ? null : page.page, x1, y1, x2, y2));
69     }
70
71     /**
72      * <p>
73      * Set the style of the field. The two parameters control the
74      * style of the text and the background style of the field. The text
75      * style must be specified, and must define a font and a fill color.
76      * The font must be a {@link StandardFont} for Acrobat 4 compatibility,
77      * or a {@link TrueTypeFont} or {@link StandardCJKFont} for Acrobat 5.
78      * The background style may specify a line and/or fill color as well as
79      * other options like
80      * {@link PDFStyle#setLineWeighting line thickness} or
81      * {@link PDFStyle#setFormStyle field style}, or may be <code>null</code>
82      * to use the defaults.
83      * </p><p>
84      * The {@link PDFStyle#setTextAlign alignment} of the text style may
85      * be left (the default), centered or right.
86      * </p>
87      * @param text the style to draw the text of the button in
88      * @param background the style to draw the background of the button in
89      * @throws IllegalArgumentException if text is <tt>null</tt> or doesn't
90      * meet the criteria above
91      * @see #setValue
92      * @see PDFStyle#setFormStyle
93      */

94     public void setStyle(PDFStyle text, PDFStyle background)
95     {
96     List l = element.getAnnotations();
97     for (int i=0;i<l.size();i++) {
98         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
99         annot.setTextStyle(text==null ? null : text.style);
100         annot.setBackgroundStyle(background==null ? null : background.style);
101     }
102     }
103
104     /**
105      * Set whether this text field is a multiline text field or not.
106      * Newly created text fields are single line fields by default.
107      * A field can only be one of password, multiline or a filename.
108      * @param multiline whether to make this field a multiline field
109      */

110     public void setMultiline(boolean multiline)
111     {
112     ((org.faceless.pdf2.FormText)element).setType(multiline ? org.faceless.pdf2.FormText.TYPE_MULTILINE : org.faceless.pdf2.FormText.TYPE_NORMAL);
113     }
114
115     /**
116      * Get whether this field is a multiline text field or not.
117      * @return true if this text field is a multiline field
118      * @see #setMultiline
119      */

120     public boolean isMultiline()
121     {
122     return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_MULTILINE;
123     }
124
125     /**
126      * For multiline text fields, set whether the field can be scrolled
127      * to enter more text than can be displayed in the form. The default
128      * value is <code>true</code>. For non-multiline text fields this
129      * method has no effect
130      * @see #isMultilineScrollable
131      * @since 1.2.1
132      */

133     public void setMultilineScrollable(boolean scrollable)
134     {
135     if (isMultiline()) {
136         ((org.faceless.pdf2.FormText)element).setScrollable(scrollable);
137     }
138     }
139
140     /**
141      * Get whether this multiline field is scrollable or not. For single
142      * line text fields, this method always returns false
143      * @see #setMultilineScrollable
144      * @since 1.2.1
145      */

146     public boolean isMultilineScrollable()
147     {
148     return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_MULTILINE && ((org.faceless.pdf2.FormText)element).isScrollable();
149     }
150
151     /**
152      * Set whether this text field is a password text field or not (the default).
153      * Password fields never return a value from {@link #getValue}, as the value
154      * is never saved in the file.
155      * A field can only be one of password, multiline or a filename.
156      * @param password whether to make this field a password field
157      */

158     public void setPassword(boolean password)
159     {
160     ((org.faceless.pdf2.FormText)element).setType(password ? org.faceless.pdf2.FormText.TYPE_PASSWORD : org.faceless.pdf2.FormText.TYPE_NORMAL);
161     }
162
163     /**
164      * Get whether this field is a password field or not.
165      * @return true if this text field is a password field
166      * @see #setPassword
167      */

168     public boolean isPassword()
169     {
170     return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_PASSWORD;
171     }
172
173     /**
174      * Set whether this field represents the name of a file, or not (the
175      * default). This feature, which is ignored in viewers prior to Acrobat
176      * 5.0 (PDF 1.4), causes the field to be treated as the name of a file,
177      * the contents of which are uploaded as the value of the field when the
178      * form is submitted. Earlier viewers will presumably just submit the
179      * filename as entered. A field can only be one of password, multiline
180      * or a filename.
181      * @param filename whether this field is a filename field
182      */

183     public void setFilename(boolean filename)
184     {
185     ((org.faceless.pdf2.FormText)element).setType(filename ? org.faceless.pdf2.FormText.TYPE_FILESELECT : org.faceless.pdf2.FormText.TYPE_NORMAL);
186     }
187
188     /**
189      * Get whether this field is a filename field or not.
190      * @return true if this text field is a filename field
191      * @see #setFilename
192      */

193     public boolean isFilename()
194     {
195     return ((org.faceless.pdf2.FormText)element).getType()==org.faceless.pdf2.FormText.TYPE_FILESELECT;
196     }
197
198     /**
199      * Set the maximum length of the field. Passing in a value of
200      * zero sets the field to have no maximum length, which is the default.
201      * @param maxlen the maximum number of characters in the field, or zero for no maximum
202      */

203     public void setMaxLength(int maxlen)
204     {
205     ((org.faceless.pdf2.FormText)element).setMaxLength(maxlen);
206     }
207
208     /**
209      * Return the maximum size of the text field, or zero if there is no maximum
210      * @return the maximum length of the text field, or zero for no maximum
211      */

212     public int getMaxLength()
213     {
214     return ((org.faceless.pdf2.FormText)element).getMaxLength();
215     }
216
217
218     /**
219      * <p>
220      * Set the value of the text field. The specified text may only contain
221      * newlines if the {@link #isMultiline} method returns true. If the
222      * {@link #isPassword} method returns true, this method throws an
223      * <code>IllegalStateException</code> - passwords may not be stored in
224      * the document, but can only be entered from Acrobat.
225      * </p>
226      * <p>
227      * Note setting fields this way will bypass any JavaScript validation
228      * that may be in effect.
229      * </p>
230      * @param value the value to set the text field to
231      * @throws IllegalArgumentException if the value contains newlines and the
232      * field is not a multiline field, or if the length is longer than
233      * <tt>getMaxLength()</tt>
234      * @throws IllegalStateException if the field is a password field
235      */

236     public void setValue(String JavaDoc value)
237         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc
238     {
239     ((org.faceless.pdf2.FormText)element).setValue(value);
240     }
241
242     /**
243      * <p>
244      * Set the default value of the text field. This is the value the field
245      * is set to if the form is reset. It does not have to be specified.
246      * </p>
247      * @param value the new default value of the text field
248      * @throws IllegalArgumentException if the value contains newlines and the field is not a multiline text field, or if the string length is longer than <tt>getMaxLength()</tt>
249      */

250     public void setDefaultValue(String JavaDoc value)
251     {
252     ((org.faceless.pdf2.FormText)element).setDefaultValue(value);
253     }
254
255     /**
256      * Return the value of the text field, or <code>null</code> if no
257      * value has been specified.
258      * @return the value of the text field or <code>null</code> if no value is set
259      */

260     public String JavaDoc getValue()
261     {
262     return ((org.faceless.pdf2.FormText)element).getValue();
263     }
264
265     /**
266      * Return the default value of the text field, or <code>null</code> if no
267      * default value has been specified.
268      * @return the default value of the text field or <code>null</code> if no default is set
269      */

270     public String JavaDoc getDefaultValue()
271     {
272     return ((org.faceless.pdf2.FormText)element).getDefaultValue();
273     }
274 }
275
Popular Tags