KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > events > FieldPositioningEvents


1 /*
2  * Copyright 2005 by Bruno Lowagie
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-2005 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-2005 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 package com.lowagie.text.pdf.events;
48
49 import java.io.IOException JavaDoc;
50 import java.util.HashMap JavaDoc;
51
52 import com.lowagie.text.Document;
53 import com.lowagie.text.DocumentException;
54 import com.lowagie.text.ExceptionConverter;
55 import com.lowagie.text.Rectangle;
56 import com.lowagie.text.pdf.PdfContentByte;
57 import com.lowagie.text.pdf.PdfFormField;
58 import com.lowagie.text.pdf.PdfName;
59 import com.lowagie.text.pdf.PdfPCell;
60 import com.lowagie.text.pdf.PdfPCellEvent;
61 import com.lowagie.text.pdf.PdfPageEventHelper;
62 import com.lowagie.text.pdf.PdfRectangle;
63 import com.lowagie.text.pdf.PdfWriter;
64 import com.lowagie.text.pdf.TextField;
65
66 /**
67  * Class for an index.
68  *
69  * @author Michael Niedermair
70  */

71 public class FieldPositioningEvents extends PdfPageEventHelper implements PdfPCellEvent {
72
73     /**
74      * Keeps a map with fields that are to be positioned in inGenericTag.
75      */

76     protected HashMap JavaDoc genericChunkFields = new HashMap JavaDoc();
77
78     /**
79      * Keeps the form field that is to be positioned in a cellLayout event.
80      */

81     protected PdfFormField cellField = null;
82     
83     /**
84      * The PdfWriter to use when a field has to added in a cell event.
85      */

86     protected PdfWriter fieldWriter = null;
87     /**
88      * The PdfFormField that is the parent of the field added in a cell event.
89      */

90     protected PdfFormField parent = null;
91     
92     /** Creates a new event. This constructor will be used if you need to position fields with Chunk objects. */
93     public FieldPositioningEvents() {}
94     
95     /** Some extra padding that will be taken into account when defining the widget. */
96     public float padding;
97     
98     /**
99      * Add a PdfFormField that has to be tied to a generic Chunk.
100      */

101     public void addField(String JavaDoc text, PdfFormField field) {
102         genericChunkFields.put(text, field);
103     }
104     
105     /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
106     public FieldPositioningEvents(PdfWriter writer, PdfFormField field) {
107         this.cellField = field;
108         this.fieldWriter = writer;
109     }
110     
111     /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event. */
112     public FieldPositioningEvents(PdfFormField parent, PdfFormField field) {
113         this.cellField = field;
114         this.parent = parent;
115     }
116     
117     /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event.
118      * @throws DocumentException
119      * @throws IOException*/

120     public FieldPositioningEvents(PdfWriter writer, String JavaDoc text) throws IOException JavaDoc, DocumentException {
121         this.fieldWriter = writer;
122         TextField tf = new TextField(writer, new Rectangle(0, 0), text);
123         tf.setFontSize(14);
124         cellField = tf.getTextField();
125     }
126     
127     /** Creates a new event. This constructor will be used if you need to position fields with a Cell Event.
128      * @throws DocumentException
129      * @throws IOException*/

130     public FieldPositioningEvents(PdfWriter writer, PdfFormField parent, String JavaDoc text) throws IOException JavaDoc, DocumentException {
131         this.parent = parent;
132         TextField tf = new TextField(writer, new Rectangle(0, 0), text);
133         tf.setFontSize(14);
134         cellField = tf.getTextField();
135     }
136
137     /**
138      * @param padding The padding to set.
139      */

140     public void setPadding(float padding) {
141         this.padding = padding;
142     }
143     
144     /**
145      * @param parent The parent to set.
146      */

147     public void setParent(PdfFormField parent) {
148         this.parent = parent;
149     }
150     /**
151      * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String)
152      */

153     public void onGenericTag(PdfWriter writer, Document document,
154             Rectangle rect, String JavaDoc text) {
155         rect.setBottom(rect.getBottom() - 3);
156         PdfFormField field = (PdfFormField) genericChunkFields.get(text);
157         if (field == null) {
158             TextField tf = new TextField(writer, new Rectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)), text);
159             tf.setFontSize(14);
160             try {
161                 field = tf.getTextField();
162             } catch (Exception JavaDoc e) {
163                 throw new ExceptionConverter(e);
164             }
165         }
166         else {
167             field.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)));
168         }
169         if (parent == null)
170             writer.addAnnotation(field);
171         else
172             parent.addKid(field);
173     }
174
175     /**
176      * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
177      */

178     public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {
179         if (cellField == null || (fieldWriter == null && parent == null)) throw new ExceptionConverter(new IllegalArgumentException JavaDoc("You have used the wrong constructor for this FieldPositioningEvents class."));
180         cellField.put(PdfName.RECT, new PdfRectangle(rect.getLeft(padding), rect.getBottom(padding), rect.getRight(padding), rect.getTop(padding)));
181         if (parent == null)
182             fieldWriter.addAnnotation(cellField);
183         else
184             parent.addKid(cellField);
185     }
186 }
Popular Tags