KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > form > PDVariableText


1 /**
2  * Copyright (c) 2004-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.interactive.form;
32
33 import org.pdfbox.cos.COSDictionary;
34 import org.pdfbox.cos.COSInteger;
35 import org.pdfbox.cos.COSName;
36 import org.pdfbox.cos.COSNumber;
37 import org.pdfbox.cos.COSString;
38 import org.pdfbox.util.BitFlagHelper;
39
40 import java.io.IOException JavaDoc;
41
42 /**
43  * A class for handling PDF fields that display text.
44  *
45  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
46  * @version $Revision: 1.7 $
47  */

48 public abstract class PDVariableText extends PDField
49 {
50     /**
51      * A Ff flag.
52      */

53     public static final int FLAG_MULTILINE = 1 << 12;
54     /**
55      * A Ff flag.
56      */

57     public static final int FLAG_PASSWORD = 1 << 13;
58     /**
59      * A Ff flag.
60      */

61     public static final int FLAG_FILE_SELECT = 1 << 20;
62     /**
63      * A Ff flag.
64      */

65     public static final int FLAG_DO_NOT_SPELL_CHECK = 1 << 22;
66     /**
67      * A Ff flag.
68      */

69     public static final int FLAG_DO_NOT_SCROLL = 1 << 23;
70     /**
71      * A Ff flag.
72      */

73     public static final int FLAG_COMB = 1 << 24;
74     /**
75      * A Ff flag.
76      */

77     public static final int FLAG_RICH_TEXT = 1 << 25;
78     
79     
80     /**
81      * DA Default appearance.
82      */

83     private COSString da;
84
85     private PDAppearance appearance;
86
87
88     /**
89      * A Q value.
90      */

91     public static final int QUADDING_LEFT = 0;
92
93     /**
94      * A Q value.
95      */

96     public static final int QUADDING_CENTERED = 1;
97
98     /**
99      * A Q value.
100      */

101     public static final int QUADDING_RIGHT = 2;
102
103     /**
104      * @see PDField#PDField(PDAcroForm,COSDictionary)
105      *
106      * @param theAcroForm The acroform.
107      */

108     public PDVariableText( PDAcroForm theAcroForm )
109     {
110         super( theAcroForm );
111     }
112     
113     /**
114      * @see org.pdfbox.pdmodel.interactive.form.PDField#PDField(PDAcroForm,COSDictionary)
115      *
116      * @param theAcroForm The acroForm for this field.
117      * @param field The field's dictionary.
118      */

119     public PDVariableText( PDAcroForm theAcroForm, COSDictionary field)
120     {
121         super( theAcroForm, field);
122         da = (COSString) field.getDictionaryObject(COSName.getPDFName("DA"));
123     }
124
125     /**
126      * @see org.pdfbox.pdmodel.interactive.form.PDField#setValue(java.lang.String)
127      *
128      * @param value The new value for this text field.
129      *
130      * @throws IOException If there is an error calculating the appearance stream.
131      */

132     public void setValue(String JavaDoc value) throws IOException JavaDoc
133     {
134         COSString fieldValue = new COSString(value);
135         getDictionary().setItem( COSName.getPDFName( "V" ), fieldValue );
136
137         //hmm, not sure what the case where the DV gets set to the field
138
//value, for now leave blank until we can come up with a case
139
//where it needs to be in there
140
//getDictionary().setItem( COSName.getPDFName( "DV" ), fieldValue );
141
if(appearance == null)
142         {
143             this.appearance = new PDAppearance( getAcroForm(), this );
144         }
145         appearance.setAppearanceValue(value);
146     }
147     
148     /**
149      * getValue gets the fields value to as a string.
150      *
151      * @return The string value of this field.
152      *
153      * @throws IOException If there is an error getting the value.
154      */

155     public String JavaDoc getValue() throws IOException JavaDoc
156     {
157         return getDictionary().getString( "V" );
158     }
159
160     /**
161      * @return true if the field is multiline
162      */

163     public boolean isMultiline()
164     {
165         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_MULTILINE );
166     }
167     
168     /**
169      * Set the multiline bit.
170      *
171      * @param multiline The value for the multiline.
172      */

173     public void setMultiline( boolean multiline )
174     {
175         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_MULTILINE, multiline );
176     }
177     
178     /**
179      * @return true if the field is a password field.
180      */

181     public boolean isPassword()
182     {
183         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_PASSWORD );
184     }
185     
186     /**
187      * Set the password bit.
188      *
189      * @param password The value for the password.
190      */

191     public void setPassword( boolean password )
192     {
193         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_PASSWORD, password );
194     }
195     
196     /**
197      * @return true if the field is a file select field.
198      */

199     public boolean isFileSelect()
200     {
201         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_FILE_SELECT );
202     }
203     
204     /**
205      * Set the file select bit.
206      *
207      * @param fileSelect The value for the fileSelect.
208      */

209     public void setFileSelect( boolean fileSelect )
210     {
211         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_FILE_SELECT, fileSelect );
212     }
213     
214     /**
215      * @return true if the field is not suppose to spell check.
216      */

217     public boolean doNotSpellCheck()
218     {
219         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_DO_NOT_SPELL_CHECK );
220     }
221     
222     /**
223      * Set the doNotSpellCheck bit.
224      *
225      * @param doNotSpellCheck The value for the doNotSpellCheck.
226      */

227     public void setDoNotSpellCheck( boolean doNotSpellCheck )
228     {
229         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_DO_NOT_SPELL_CHECK, doNotSpellCheck );
230     }
231     
232     /**
233      * @return true if the field is not suppose to scroll.
234      */

235     public boolean doNotScroll()
236     {
237         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_DO_NOT_SCROLL );
238     }
239     
240     /**
241      * Set the doNotScroll bit.
242      *
243      * @param doNotScroll The value for the doNotScroll.
244      */

245     public void setDoNotScroll( boolean doNotScroll )
246     {
247         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_DO_NOT_SCROLL, doNotScroll );
248     }
249     
250     /**
251      * @return true if the field is not suppose to comb the text display.
252      */

253     public boolean shouldComb()
254     {
255         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_COMB );
256     }
257     
258     /**
259      * Set the comb bit.
260      *
261      * @param comb The value for the comb.
262      */

263     public void setComb( boolean comb )
264     {
265         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_COMB, comb );
266     }
267     
268     /**
269      * @return true if the field is a rich text field.
270      */

271     public boolean isRichText()
272     {
273         return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_RICH_TEXT );
274     }
275     
276     /**
277      * Set the richText bit.
278      *
279      * @param richText The value for the richText.
280      */

281     public void setRichText( boolean richText )
282     {
283         BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_RICH_TEXT, richText );
284     }
285
286     /**
287      * @return the DA element of the dictionary object
288      */

289     protected COSString getDefaultAppearance()
290     {
291         return da;
292     }
293
294     /**
295      * This will get the 'quadding' or justification of the text to be displayed.
296      * 0 - Left(default)<br/>
297      * 1 - Centered<br />
298      * 2 - Right<br />
299      * Please see the QUADDING_CONSTANTS.
300      *
301      * @return The justification of the text strings.
302      */

303     public int getQ()
304     {
305         int retval = 0;
306         COSNumber number = (COSNumber)getDictionary().getDictionaryObject( COSName.getPDFName( "Q" ) );
307         if( number != null )
308         {
309             retval = number.intValue();
310         }
311         return retval;
312     }
313
314     /**
315      * This will set the quadding/justification of the text. See QUADDING constants.
316      *
317      * @param q The new text justification.
318      */

319     public void setQ( int q )
320     {
321         getDictionary().setItem( COSName.getPDFName( "Q" ), new COSInteger( q ) );
322     }
323
324 }
Popular Tags