KickJava   Java API By Example, From Geeks To Geeks.

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


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

52
53 package com.lowagie.text.pdf;
54
55 import java.util.ArrayList JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.Iterator JavaDoc;
58
59 /**
60  * This class captures an AcroForm on input. Basically, it extends Dictionary
61  * by indexing the fields of an AcroForm
62  * @author Mark Thompson
63  */

64
65 public class PRAcroForm extends PdfDictionary {
66     
67     /**
68      * This class holds the information for a single field
69      */

70     public static class FieldInformation {
71         String JavaDoc name;
72         PdfDictionary info;
73         PRIndirectReference ref;
74         
75         FieldInformation(String JavaDoc name, PdfDictionary info, PRIndirectReference ref) {
76             this.name = name; this.info = info; this.ref = ref;
77         }
78         public String JavaDoc getName() { return name; }
79         public PdfDictionary getInfo() { return info; }
80         public PRIndirectReference getRef() { return ref; }
81     };
82     ArrayList JavaDoc fields;
83     ArrayList JavaDoc stack;
84     HashMap JavaDoc fieldByName;
85     PdfReader reader;
86     
87     /**
88      * Constructor
89      * @param reader reader of the input file
90      */

91     public PRAcroForm(PdfReader reader) {
92         this.reader = reader;
93         fields = new ArrayList JavaDoc();
94         fieldByName = new HashMap JavaDoc();
95         stack = new ArrayList JavaDoc();
96     }
97     /**
98      * Number of fields found
99      * @return size
100      */

101     public int size() {
102         return fields.size();
103     }
104     
105     public ArrayList JavaDoc getFields() {
106         return fields;
107     }
108     
109     public FieldInformation getField(String JavaDoc name) {
110         return (FieldInformation)fieldByName.get(name);
111     }
112     
113     /**
114      * Given the title (/T) of a reference, return the associated reference
115      * @param name a string containing the path
116      * @return a reference to the field, or null
117      */

118     public PRIndirectReference getRefByName(String JavaDoc name) {
119         FieldInformation fi = (FieldInformation)fieldByName.get(name);
120         if (fi == null) return null;
121         return fi.getRef();
122     }
123     /**
124      * Read, and comprehend the acroform
125      * @param root the docment root
126      */

127     public void readAcroForm(PdfDictionary root) {
128         if (root == null)
129             return;
130         hashMap = root.hashMap;
131         pushAttrib(root);
132         PdfArray fieldlist = (PdfArray)PdfReader.getPdfObjectRelease(root.get(PdfName.FIELDS));
133         iterateFields(fieldlist, null, null);
134     }
135     
136     /**
137      * After reading, we index all of the fields. Recursive.
138      * @param fieldlist An array of fields
139      * @param fieldDict the last field dictionary we encountered (recursively)
140      * @param title the pathname of the field, up to this point or null
141      */

142     protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String JavaDoc title) {
143         for (Iterator JavaDoc it = fieldlist.getArrayList().iterator(); it.hasNext();) {
144             PRIndirectReference ref = (PRIndirectReference)it.next();
145             PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
146             
147             // if we are not a field dictionary, pass our parent's values
148
PRIndirectReference myFieldDict = fieldDict;
149             String JavaDoc myTitle = title;
150             PdfString tField = (PdfString)dict.get(PdfName.T);
151             boolean isFieldDict = tField != null;
152             
153             if (isFieldDict) {
154                 myFieldDict = ref;
155                 if (title == null) myTitle = tField.toString();
156                 else myTitle = title + '.' + tField.toString();
157             }
158             
159             PdfArray kids = (PdfArray)dict.get(PdfName.KIDS);
160             if (kids != null) {
161                 pushAttrib(dict);
162                 iterateFields(kids, myFieldDict, myTitle);
163                 stack.remove(stack.size() - 1); // pop
164
}
165             else { // leaf node
166
if (myFieldDict != null) {
167                     PdfDictionary mergedDict = (PdfDictionary)stack.get(stack.size() - 1);
168                     if (isFieldDict)
169                         mergedDict = mergeAttrib(mergedDict, dict);
170                     
171                     mergedDict.put(PdfName.T, new PdfString(myTitle));
172                     FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict);
173                     fields.add(fi);
174                     fieldByName.put(myTitle, fi);
175                 }
176             }
177         }
178     }
179     /**
180      * merge field attributes from two dictionaries
181      * @param parent one dictionary
182      * @param child the other dictionary
183      * @return a merged dictionary
184      */

185     protected PdfDictionary mergeAttrib(PdfDictionary parent, PdfDictionary child) {
186         PdfDictionary targ = new PdfDictionary();
187         if (parent != null) targ.putAll(parent);
188         
189         for (Iterator JavaDoc it = child.getKeys().iterator(); it.hasNext();) {
190             PdfName key = (PdfName) it.next();
191             if (key.equals(PdfName.DR) || key.equals(PdfName.DA) ||
192             key.equals(PdfName.Q) || key.equals(PdfName.FF) ||
193             key.equals(PdfName.DV) || key.equals(PdfName.V)
194             || key.equals(PdfName.FT)
195             || key.equals(PdfName.F)) {
196                 targ.put(key,child.get(key));
197             }
198         }
199         return targ;
200     }
201     /**
202      * stack a level of dictionary. Merge in a dictionary from this level
203      */

204     protected void pushAttrib(PdfDictionary dict) {
205         PdfDictionary dic = null;
206         if (!stack.isEmpty()) {
207             dic = (PdfDictionary)stack.get(stack.size() - 1);
208         }
209         dic = mergeAttrib(dic, dict);
210         stack.add(dic);
211     }
212 }
213
Popular Tags