2
* $Id: PRAcroForm.java 3373 2008-05-12 16:21:24Z xlv $
4
* Copyright 2001, 2002 by Paulo Soares.
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* (the "License"); you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the License.
14
* The Original Code is 'iText, a free JAVA-PDF library'.
16
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18
* All Rights Reserved.
19
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22
* This class written by Mark Thompson, Copyright (C) 2002 by Mark Thompson.
24
* Contributor(s): all the names of the contributors are added in the source code
27
* Alternatively, the contents of this file may be used under the terms of the
28
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
29
* provisions of LGPL are applicable instead of those above. If you wish to
30
* allow use of your version of this file only under the terms of the LGPL
31
* License and not to allow others to use your version of this file under
32
* the MPL, indicate your decision by deleting the provisions above and
33
* replace them with the notice and other provisions required by the LGPL.
34
* If you do not delete the provisions above, a recipient may use your version
35
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
37
* This library is free software; you can redistribute it and/or modify it
38
* under the terms of the MPL as stated above or under the terms of the GNU
39
* Library General Public License as published by the Free Software Foundation;
40
* either version 2 of the License, or any later version.
42
* This library is distributed in the hope that it will be useful, but WITHOUT
43
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
44
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
47
* If you didn't download this code from the following link, you should check if
48
* you aren't using an obsolete version:
49
* http://www.lowagie.com/iText/
52
package com.lowagie.text.pdf;
54
import java.util.ArrayList;
55
import java.util.HashMap;
56
import java.util.Iterator;
59
* This class captures an AcroForm on input. Basically, it extends Dictionary
60
* by indexing the fields of an AcroForm
61
* @author Mark Thompson
64
public class PRAcroForm extends PdfDictionary {
67
* This class holds the information for a single field
69
public static class FieldInformation {
72
PRIndirectReference ref;
74
FieldInformation(String name, PdfDictionary info, PRIndirectReference ref) {
75
this.name = name; this.info = info; this.ref = ref;
77
public String getName() { return name; }
78
public PdfDictionary getInfo() { return info; }
79
public PRIndirectReference getRef() { return ref; }
88
* @param reader reader of the input file
90
public PRAcroForm(PdfReader reader) {
92
fields = new ArrayList();
93
fieldByName = new HashMap();
94
stack = new ArrayList();
97
* Number of fields found
101
return fields.size();
104
public ArrayList getFields() {
108
public FieldInformation getField(String name) {
109
return (FieldInformation)fieldByName.get(name);
113
* Given the title (/T) of a reference, return the associated reference
114
* @param name a string containing the path
115
* @return a reference to the field, or null
117
public PRIndirectReference getRefByName(String name) {
118
FieldInformation fi = (FieldInformation)fieldByName.get(name);
119
if (fi == null) return null;
123
* Read, and comprehend the acroform
124
* @param root the document root
126
public void readAcroForm(PdfDictionary root) {
129
hashMap = root.hashMap;
131
PdfArray fieldlist = (PdfArray)PdfReader.getPdfObjectRelease(root.get(PdfName.FIELDS));
132
iterateFields(fieldlist, null, null);
136
* After reading, we index all of the fields. Recursive.
137
* @param fieldlist An array of fields
138
* @param fieldDict the last field dictionary we encountered (recursively)
139
* @param title the pathname of the field, up to this point or null
141
protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String title) {
142
for (Iterator it = fieldlist.getArrayList().iterator(); it.hasNext();) {
143
PRIndirectReference ref = (PRIndirectReference)it.next();
144
PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
146
// if we are not a field dictionary, pass our parent's values
147
PRIndirectReference myFieldDict = fieldDict;
148
String myTitle = title;
149
PdfString tField = (PdfString)dict.get(PdfName.T);
150
boolean isFieldDict = tField != null;
154
if (title == null) myTitle = tField.toString();
155
else myTitle = title + '.' + tField.toString();
158
PdfArray kids = (PdfArray)dict.get(PdfName.KIDS);
161
iterateFields(kids, myFieldDict, myTitle);
162
stack.remove(stack.size() - 1); // pop
165
if (myFieldDict != null) {
166
PdfDictionary mergedDict = (PdfDictionary)stack.get(stack.size() - 1);
168
mergedDict = mergeAttrib(mergedDict, dict);
170
mergedDict.put(PdfName.T, new PdfString(myTitle));
171
FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict);
173
fieldByName.put(myTitle, fi);
179
* merge field attributes from two dictionaries
180
* @param parent one dictionary
181
* @param child the other dictionary
182
* @return a merged dictionary
184
protected PdfDictionary mergeAttrib(PdfDictionary parent, PdfDictionary child) {
185
PdfDictionary targ = new PdfDictionary();
186
if (parent != null) targ.putAll(parent);
188
for (Iterator it = child.getKeys().iterator(); it.hasNext();) {
189
PdfName key = (PdfName) it.next();
190
if (key.equals(PdfName.DR) || key.equals(PdfName.DA) ||
191
key.equals(PdfName.Q) || key.equals(PdfName.FF) ||
192
key.equals(PdfName.DV) || key.equals(PdfName.V)
193
|| key.equals(PdfName.FT)
194
|| key.equals(PdfName.F)) {
195
targ.put(key,child.get(key));
201
* stack a level of dictionary. Merge in a dictionary from this level
203
protected void pushAttrib(PdfDictionary dict) {
204
PdfDictionary dic = null;
205
if (!stack.isEmpty()) {
206
dic = (PdfDictionary)stack.get(stack.size() - 1);
208
dic = mergeAttrib(dic, dict);