KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002 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 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51
52 import com.lowagie.text.Rectangle;
53
54 /** Implements form fields.
55  *
56  * @author Paulo Soares (psoares@consiste.pt)
57  */

58 public class PdfFormField extends PdfAnnotation {
59
60     public static final int FF_READ_ONLY = 1;
61     public static final int FF_REQUIRED = 2;
62     public static final int FF_NO_EXPORT = 4;
63     public static final int FF_NO_TOGGLE_TO_OFF = 16384;
64     public static final int FF_RADIO = 32768;
65     public static final int FF_PUSHBUTTON = 65536;
66     public static final int FF_MULTILINE = 4096;
67     public static final int FF_PASSWORD = 8192;
68     public static final int FF_COMBO = 131072;
69     public static final int FF_EDIT = 262144;
70     public static final int FF_FILESELECT = 1048576;
71     public static final int FF_MULTISELECT = 2097152;
72     public static final int FF_DONOTSPELLCHECK = 4194304;
73     public static final int FF_DONOTSCROLL = 8388608;
74     public static final int FF_COMB = 16777216;
75     public static final int FF_RADIOSINUNISON = 1 << 25;
76     public static final int Q_LEFT = 0;
77     public static final int Q_CENTER = 1;
78     public static final int Q_RIGHT = 2;
79     public static final int MK_NO_ICON = 0;
80     public static final int MK_NO_CAPTION = 1;
81     public static final int MK_CAPTION_BELOW = 2;
82     public static final int MK_CAPTION_ABOVE = 3;
83     public static final int MK_CAPTION_RIGHT = 4;
84     public static final int MK_CAPTION_LEFT = 5;
85     public static final int MK_CAPTION_OVERLAID = 6;
86     public static final PdfName IF_SCALE_ALWAYS = PdfName.A;
87     public static final PdfName IF_SCALE_BIGGER = PdfName.B;
88     public static final PdfName IF_SCALE_SMALLER = PdfName.S;
89     public static final PdfName IF_SCALE_NEVER = PdfName.N;
90     public static final PdfName IF_SCALE_ANAMORPHIC = PdfName.A;
91     public static final PdfName IF_SCALE_PROPORTIONAL = PdfName.P;
92     public static final boolean MULTILINE = true;
93     public static final boolean SINGLELINE = false;
94     public static final boolean PLAINTEXT = false;
95     public static final boolean PASSWORD = true;
96     static PdfName mergeTarget[] = {PdfName.FONT, PdfName.XOBJECT, PdfName.COLORSPACE, PdfName.PATTERN};
97     
98     /** Holds value of property parent. */
99     protected PdfFormField parent;
100     
101     protected ArrayList JavaDoc kids;
102     
103 /**
104  * Constructs a new <CODE>PdfAnnotation</CODE> of subtype link (Action).
105  */

106     
107     public PdfFormField(PdfWriter writer, float llx, float lly, float urx, float ury, PdfAction action) {
108         super(writer, llx, lly, urx, ury, action);
109         put(PdfName.TYPE, PdfName.ANNOT);
110         put(PdfName.SUBTYPE, PdfName.WIDGET);
111         annotation = true;
112     }
113
114     /** Creates new PdfFormField */
115     protected PdfFormField(PdfWriter writer) {
116         super(writer, null);
117         form = true;
118         annotation = false;
119     }
120     
121     public void setWidget(Rectangle rect, PdfName highlight) {
122         put(PdfName.TYPE, PdfName.ANNOT);
123         put(PdfName.SUBTYPE, PdfName.WIDGET);
124         put(PdfName.RECT, new PdfRectangle(rect));
125         annotation = true;
126         if (highlight != null && !highlight.equals(HIGHLIGHT_INVERT))
127             put(PdfName.H, highlight);
128     }
129     
130     public static PdfFormField createEmpty(PdfWriter writer) {
131         PdfFormField field = new PdfFormField(writer);
132         return field;
133     }
134     
135     public void setButton(int flags) {
136         put(PdfName.FT, PdfName.BTN);
137         if (flags != 0)
138             put(PdfName.FF, new PdfNumber(flags));
139     }
140     
141     protected static PdfFormField createButton(PdfWriter writer, int flags) {
142         PdfFormField field = new PdfFormField(writer);
143         field.setButton(flags);
144         return field;
145     }
146     
147     public static PdfFormField createPushButton(PdfWriter writer) {
148         return createButton(writer, FF_PUSHBUTTON);
149     }
150
151     public static PdfFormField createCheckBox(PdfWriter writer) {
152         return createButton(writer, 0);
153     }
154
155     public static PdfFormField createRadioButton(PdfWriter writer, boolean noToggleToOff) {
156         return createButton(writer, FF_RADIO + (noToggleToOff ? FF_NO_TOGGLE_TO_OFF : 0));
157     }
158     
159     public static PdfFormField createTextField(PdfWriter writer, boolean multiline, boolean password, int maxLen) {
160         PdfFormField field = new PdfFormField(writer);
161         field.put(PdfName.FT, PdfName.TX);
162         int flags = (multiline ? FF_MULTILINE : 0);
163         flags += (password ? FF_PASSWORD : 0);
164         field.put(PdfName.FF, new PdfNumber(flags));
165         if (maxLen > 0)
166             field.put(PdfName.MAXLEN, new PdfNumber(maxLen));
167         return field;
168     }
169     
170     protected static PdfFormField createChoice(PdfWriter writer, int flags, PdfArray options, int topIndex) {
171         PdfFormField field = new PdfFormField(writer);
172         field.put(PdfName.FT, PdfName.CH);
173         field.put(PdfName.FF, new PdfNumber(flags));
174         field.put(PdfName.OPT, options);
175         if (topIndex > 0)
176             field.put(PdfName.TI, new PdfNumber(topIndex));
177         return field;
178     }
179     
180     public static PdfFormField createList(PdfWriter writer, String JavaDoc options[], int topIndex) {
181         return createChoice(writer, 0, processOptions(options), topIndex);
182     }
183
184     public static PdfFormField createList(PdfWriter writer, String JavaDoc options[][], int topIndex) {
185         return createChoice(writer, 0, processOptions(options), topIndex);
186     }
187
188     public static PdfFormField createCombo(PdfWriter writer, boolean edit, String JavaDoc options[], int topIndex) {
189         return createChoice(writer, FF_COMBO + (edit ? FF_EDIT : 0), processOptions(options), topIndex);
190     }
191     
192     public static PdfFormField createCombo(PdfWriter writer, boolean edit, String JavaDoc options[][], int topIndex) {
193         return createChoice(writer, FF_COMBO + (edit ? FF_EDIT : 0), processOptions(options), topIndex);
194     }
195     
196     protected static PdfArray processOptions(String JavaDoc options[]) {
197         PdfArray array = new PdfArray();
198         for (int k = 0; k < options.length; ++k) {
199             array.add(new PdfString(options[k], PdfObject.TEXT_UNICODE));
200         }
201         return array;
202     }
203     
204     protected static PdfArray processOptions(String JavaDoc options[][]) {
205         PdfArray array = new PdfArray();
206         for (int k = 0; k < options.length; ++k) {
207             String JavaDoc subOption[] = options[k];
208             PdfArray ar2 = new PdfArray(new PdfString(subOption[0], PdfObject.TEXT_UNICODE));
209             ar2.add(new PdfString(subOption[1], PdfObject.TEXT_UNICODE));
210             array.add(ar2);
211         }
212         return array;
213     }
214     
215     public static PdfFormField createSignature(PdfWriter writer) {
216         PdfFormField field = new PdfFormField(writer);
217         field.put(PdfName.FT, PdfName.SIG);
218         return field;
219     }
220     
221     /** Getter for property parent.
222      * @return Value of property parent.
223      */

224     public PdfFormField getParent() {
225         return parent;
226     }
227     
228     public void addKid(PdfFormField field) {
229         field.parent = this;
230         if (kids == null)
231             kids = new ArrayList JavaDoc();
232         kids.add(field);
233     }
234     
235     public ArrayList JavaDoc getKids() {
236         return kids;
237     }
238     
239     public int setFieldFlags(int flags) {
240         PdfNumber obj = (PdfNumber)get(PdfName.FF);
241         int old;
242         if (obj == null)
243             old = 0;
244         else
245             old = obj.intValue();
246         int v = old | flags;
247         put(PdfName.FF, new PdfNumber(v));
248         return old;
249     }
250     
251     public void setValueAsString(String JavaDoc s) {
252         put(PdfName.V, new PdfString(s, PdfObject.TEXT_UNICODE));
253     }
254
255     public void setValueAsName(String JavaDoc s) {
256         put(PdfName.V, new PdfName(s));
257     }
258
259     public void setValue(PdfSignature sig) {
260         put(PdfName.V, sig);
261     }
262
263     public void setDefaultValueAsString(String JavaDoc s) {
264         put(PdfName.DV, new PdfString(s, PdfObject.TEXT_UNICODE));
265     }
266
267     public void setDefaultValueAsName(String JavaDoc s) {
268         put(PdfName.DV, new PdfName(s));
269     }
270     
271     public void setFieldName(String JavaDoc s) {
272         if (s != null)
273             put(PdfName.T, new PdfString(s, PdfObject.TEXT_UNICODE));
274     }
275     
276     public void setUserName(String JavaDoc s) {
277         put(PdfName.TU, new PdfString(s, PdfObject.TEXT_UNICODE));
278     }
279     
280     public void setMappingName(String JavaDoc s) {
281         put(PdfName.TM, new PdfString(s, PdfObject.TEXT_UNICODE));
282     }
283     
284     public void setQuadding(int v) {
285         put(PdfName.Q, new PdfNumber(v));
286     }
287     
288     static void mergeResources(PdfDictionary result, PdfDictionary source, PdfStamperImp writer) {
289         PdfDictionary dic = null;
290         PdfDictionary res = null;
291         PdfName target = null;
292         for (int k = 0; k < mergeTarget.length; ++k) {
293             target = mergeTarget[k];
294             PdfDictionary pdfDict = (PdfDictionary)PdfReader.getPdfObject(source.get(target));
295             if ((dic = pdfDict) != null) {
296                 if ((res = (PdfDictionary)PdfReader.getPdfObject(result.get(target), result)) == null) {
297                     res = new PdfDictionary();
298                 }
299                 res.mergeDifferent(dic);
300                 result.put(target, res);
301                 if (writer != null)
302                     writer.markUsed(res);
303             }
304         }
305     }
306
307     static void mergeResources(PdfDictionary result, PdfDictionary source) {
308         mergeResources(result, source, null);
309     }
310
311     public void setUsed() {
312         used = true;
313         if (parent != null)
314             put(PdfName.PARENT, parent.getIndirectReference());
315         if (kids != null) {
316             PdfArray array = new PdfArray();
317             for (int k = 0; k < kids.size(); ++k)
318                 array.add(((PdfFormField)kids.get(k)).getIndirectReference());
319             put(PdfName.KIDS, array);
320         }
321         if (templates == null)
322             return;
323         PdfDictionary dic = new PdfDictionary();
324         for (Iterator JavaDoc it = templates.keySet().iterator(); it.hasNext();) {
325             PdfTemplate template = (PdfTemplate)it.next();
326             mergeResources(dic, (PdfDictionary)template.getResources());
327         }
328         put(PdfName.DR, dic);
329     }
330
331     public static PdfAnnotation shallowDuplicate(PdfAnnotation annot) {
332         PdfAnnotation dup;
333         if (annot.isForm()) {
334             dup = new PdfFormField(annot.writer);
335             PdfFormField dupField = (PdfFormField)dup;
336             PdfFormField srcField = (PdfFormField)annot;
337             dupField.parent = srcField.parent;
338             dupField.kids = srcField.kids;
339         }
340         else
341             dup = new PdfAnnotation(annot.writer, null);
342         dup.merge(annot);
343         dup.form = annot.form;
344         dup.annotation = annot.annotation;
345         dup.templates = annot.templates;
346         return dup;
347     }
348 }
349
Popular Tags