001/*
002 * $Id: PRAcroForm.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.pdf;
045
046import java.util.ArrayList;
047import java.util.HashMap;
048import java.util.Iterator;
049
050/**
051 * This class captures an AcroForm on input. Basically, it extends Dictionary
052 * by indexing the fields of an AcroForm
053 * @author Mark Thompson
054 */
055
056public class PRAcroForm extends PdfDictionary {
057
058    /**
059     * This class holds the information for a single field
060     */
061    public static class FieldInformation {
062        String name;
063        PdfDictionary info;
064        PRIndirectReference ref;
065
066        FieldInformation(String name, PdfDictionary info, PRIndirectReference ref) {
067            this.name = name; this.info = info; this.ref = ref;
068        }
069        public String getName() { return name; }
070        public PdfDictionary getInfo() { return info; }
071        public PRIndirectReference getRef() { return ref; }
072    };
073    ArrayList<FieldInformation> fields;
074    ArrayList<PdfDictionary> stack;
075    HashMap<String, FieldInformation> fieldByName;
076    PdfReader reader;
077
078    /**
079     * Constructor
080     * @param reader reader of the input file
081     */
082    public PRAcroForm(PdfReader reader) {
083        this.reader = reader;
084        fields = new ArrayList<FieldInformation>();
085        fieldByName = new HashMap<String, FieldInformation>();
086        stack = new ArrayList<PdfDictionary>();
087    }
088    /**
089     * Number of fields found
090     * @return size
091     */
092    @Override
093    public int size() {
094        return fields.size();
095    }
096
097    public ArrayList<FieldInformation> getFields() {
098        return fields;
099    }
100
101    public FieldInformation getField(String name) {
102        return fieldByName.get(name);
103    }
104
105    /**
106     * Given the title (/T) of a reference, return the associated reference
107     * @param name a string containing the path
108     * @return a reference to the field, or null
109     */
110    public PRIndirectReference getRefByName(String name) {
111        FieldInformation fi = fieldByName.get(name);
112        if (fi == null) return null;
113        return fi.getRef();
114    }
115    /**
116     * Read, and comprehend the acroform
117     * @param root the document root
118     */
119    public void readAcroForm(PdfDictionary root) {
120        if (root == null)
121            return;
122        hashMap = root.hashMap;
123        pushAttrib(root);
124        PdfArray fieldlist = (PdfArray)PdfReader.getPdfObjectRelease(root.get(PdfName.FIELDS));
125        iterateFields(fieldlist, null, null);
126    }
127
128    /**
129     * After reading, we index all of the fields. Recursive.
130     * @param fieldlist An array of fields
131     * @param fieldDict the last field dictionary we encountered (recursively)
132     * @param title the pathname of the field, up to this point or null
133     */
134    protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String title) {
135        for (Iterator<PdfObject> it = fieldlist.listIterator(); it.hasNext();) {
136            PRIndirectReference ref = (PRIndirectReference)it.next();
137            PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref);
138
139            // if we are not a field dictionary, pass our parent's values
140            PRIndirectReference myFieldDict = fieldDict;
141            String myTitle = title;
142            PdfString tField = (PdfString)dict.get(PdfName.T);
143            boolean isFieldDict = tField != null;
144
145            if (isFieldDict) {
146                myFieldDict = ref;
147                if (title == null) myTitle = tField.toString();
148                else myTitle = title + '.' + tField.toString();
149            }
150
151            PdfArray kids = (PdfArray)dict.get(PdfName.KIDS);
152            if (kids != null) {
153                pushAttrib(dict);
154                iterateFields(kids, myFieldDict, myTitle);
155                stack.remove(stack.size() - 1);   // pop
156            }
157            else {          // leaf node
158                if (myFieldDict != null) {
159                    PdfDictionary mergedDict = stack.get(stack.size() - 1);
160                    if (isFieldDict)
161                        mergedDict = mergeAttrib(mergedDict, dict);
162
163                    mergedDict.put(PdfName.T, new PdfString(myTitle));
164                    FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict);
165                    fields.add(fi);
166                    fieldByName.put(myTitle, fi);
167                }
168            }
169        }
170    }
171    /**
172     * merge field attributes from two dictionaries
173     * @param parent one dictionary
174     * @param child the other dictionary
175     * @return a merged dictionary
176     */
177    protected PdfDictionary mergeAttrib(PdfDictionary parent, PdfDictionary child) {
178        PdfDictionary targ = new PdfDictionary();
179        if (parent != null) targ.putAll(parent);
180
181        for (Object element : child.getKeys()) {
182            PdfName key = (PdfName) element;
183            if (key.equals(PdfName.DR) || key.equals(PdfName.DA) ||
184            key.equals(PdfName.Q)  || key.equals(PdfName.FF) ||
185            key.equals(PdfName.DV) || key.equals(PdfName.V)
186            || key.equals(PdfName.FT)
187            || key.equals(PdfName.F)) {
188                targ.put(key,child.get(key));
189            }
190        }
191        return targ;
192    }
193    /**
194     * stack a level of dictionary. Merge in a dictionary from this level
195     */
196    protected void pushAttrib(PdfDictionary dict) {
197        PdfDictionary dic = null;
198        if (!stack.isEmpty()) {
199            dic = stack.get(stack.size() - 1);
200        }
201        dic = mergeAttrib(dic, dict);
202        stack.add(dic);
203    }
204}