2
* Copyright 2003 by Paulo Soares.
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/
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.
12
* The Original Code is 'iText, a free JAVA-PDF library'.
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.
20
* Contributor(s): all the names of the contributors are added in the source code
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.
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.
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
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/
47
package com.lowagie.text.pdf;
48
import java.io.IOException;
49
import java.io.InputStream;
50
import java.util.HashMap;
51
import java.util.ArrayList;
53
/** Reads an FDF form and makes the fields available
54
* @author Paulo Soares (psoares@consiste.pt)
56
public class FdfReader extends PdfReader {
62
/** Reads an FDF form.
63
* @param filename the file name of the form
64
* @throws IOException on error
66
public FdfReader(String filename) throws IOException {
70
/** Reads an FDF form.
71
* @param pdfIn the byte array with the form
72
* @throws IOException on error
74
public FdfReader(byte pdfIn[]) throws IOException {
78
/** Reads an FDF form.
79
* @param url the URL of the document
80
* @throws IOException on error
82
public FdfReader(URL url) throws IOException {
86
/** Reads an FDF form.
87
* @param is the <CODE>InputStream</CODE> containing the document. The stream is read to the
88
* end but is not closed
89
* @throws IOException on error
91
public FdfReader(InputStream is) throws IOException {
95
protected void readPdf() throws IOException {
96
fields = new HashMap();
98
tokens.checkFdfHeader();
106
catch (Exception e) {
113
protected void kidNode(PdfDictionary merged, String name) {
114
PdfArray kids = (PdfArray)getPdfObject(merged.get(PdfName.KIDS));
115
if (kids == null || kids.getArrayList().size() == 0) {
116
if (name.length() > 0)
117
name = name.substring(1);
118
fields.put(name, merged);
121
merged.remove(PdfName.KIDS);
122
ArrayList ar = kids.getArrayList();
123
for (int k = 0; k < ar.size(); ++k) {
124
PdfDictionary dic = new PdfDictionary();
126
PdfDictionary newDic = (PdfDictionary)getPdfObject((PdfObject)ar.get(k));
127
PdfString t = (PdfString)getPdfObject(newDic.get(PdfName.T));
128
String newName = name;
130
newName += "." + t.toUnicodeString();
132
dic.remove(PdfName.T);
133
kidNode(dic, newName);
138
protected void readFields() throws IOException {
139
catalog = (PdfDictionary)getPdfObject(trailer.get(PdfName.ROOT));
140
PdfDictionary fdf = (PdfDictionary)getPdfObject(catalog.get(PdfName.FDF));
141
PdfString fs = (PdfString)getPdfObject(fdf.get(PdfName.F));
143
fileSpec = fs.toUnicodeString();
144
PdfArray fld = (PdfArray)getPdfObject(fdf.get(PdfName.FIELDS));
147
encoding = (PdfName)getPdfObject(fdf.get(PdfName.ENCODING));
148
PdfDictionary merged = new PdfDictionary();
149
merged.put(PdfName.KIDS, fld);
153
/** Gets all the fields. The map is keyed by the fully qualified
154
* field name and the value is a merged <CODE>PdfDictionary</CODE>
155
* with the field content.
156
* @return all the fields
158
public HashMap getFields() {
162
/** Gets the field dictionary.
163
* @param name the fully qualified field name
164
* @return the field dictionary
166
public PdfDictionary getField(String name) {
167
return (PdfDictionary)fields.get(name);
170
/** Gets the field value or <CODE>null</CODE> if the field does not
171
* exist or has no value defined.
172
* @param name the fully qualified field name
173
* @return the field value or <CODE>null</CODE>
175
public String getFieldValue(String name) {
176
PdfDictionary field = (PdfDictionary)fields.get(name);
179
PdfObject v = getPdfObject(field.get(PdfName.V));
183
return PdfName.decodeName(((PdfName)v).toString());
184
else if (v.isString()) {
185
PdfString vs = (PdfString)v;
186
if (encoding == null || vs.getEncoding() != null)
187
return vs.toUnicodeString();
188
byte b[] = vs.getBytes();
189
if (b.length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
190
return vs.toUnicodeString();
192
if (encoding.equals(PdfName.SHIFT_JIS))
193
return new String(b, "SJIS");
194
else if (encoding.equals(PdfName.UHC))
195
return new String(b, "MS949");
196
else if (encoding.equals(PdfName.GBK))
197
return new String(b, "GBK");
198
else if (encoding.equals(PdfName.BIGFIVE))
199
return new String(b, "Big5");
201
catch (Exception e) {
203
return vs.toUnicodeString();
208
/** Gets the PDF file specification contained in the FDF.
209
* @return the PDF file specification contained in the FDF
211
public String getFileSpec() {
b'\\ No newline at end of file'