1 47 package com.lowagie.text.pdf; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.net.URL ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 56 public class FdfReader extends PdfReader { 57 58 HashMap fields; 59 String fileSpec; 60 PdfName encoding; 61 62 66 public FdfReader(String filename) throws IOException { 67 super(filename); 68 } 69 70 74 public FdfReader(byte pdfIn[]) throws IOException { 75 super(pdfIn); 76 } 77 78 82 public FdfReader(URL url) throws IOException { 83 super(url); 84 } 85 86 91 public FdfReader(InputStream is) throws IOException { 92 super(is); 93 } 94 95 protected void readPdf() throws IOException { 96 fields = new HashMap (); 97 try { 98 tokens.checkFdfHeader(); 99 rebuildXref(); 100 readDocObj(); 101 } 102 finally { 103 try { 104 tokens.close(); 105 } 106 catch (Exception e) { 107 } 109 } 110 readFields(); 111 } 112 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); 119 } 120 else { 121 merged.remove(PdfName.KIDS); 122 ArrayList ar = kids.getArrayList(); 123 for (int k = 0; k < ar.size(); ++k) { 124 PdfDictionary dic = new PdfDictionary(); 125 dic.merge(merged); 126 PdfDictionary newDic = (PdfDictionary)getPdfObject((PdfObject)ar.get(k)); 127 PdfString t = (PdfString)getPdfObject(newDic.get(PdfName.T)); 128 String newName = name; 129 if (t != null) 130 newName += "." + t.toUnicodeString(); 131 dic.merge(newDic); 132 dic.remove(PdfName.T); 133 kidNode(dic, newName); 134 } 135 } 136 } 137 138 protected void readFields() { 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)); 142 if (fs != null) 143 fileSpec = fs.toUnicodeString(); 144 PdfArray fld = (PdfArray)getPdfObject(fdf.get(PdfName.FIELDS)); 145 if (fld == null) 146 return; 147 encoding = (PdfName)getPdfObject(fdf.get(PdfName.ENCODING)); 148 PdfDictionary merged = new PdfDictionary(); 149 merged.put(PdfName.KIDS, fld); 150 kidNode(merged, ""); 151 } 152 153 158 public HashMap getFields() { 159 return fields; 160 } 161 162 166 public PdfDictionary getField(String name) { 167 return (PdfDictionary)fields.get(name); 168 } 169 170 175 public String getFieldValue(String name) { 176 PdfDictionary field = (PdfDictionary)fields.get(name); 177 if (field == null) 178 return null; 179 PdfObject v = getPdfObject(field.get(PdfName.V)); 180 if (v == null) 181 return null; 182 if (v.isName()) 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(); 191 try { 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"); 200 } 201 catch (Exception e) { 202 } 203 return vs.toUnicodeString(); 204 } 205 return null; 206 } 207 208 211 public String getFileSpec() { 212 return fileSpec; 213 } 214 } | Popular Tags |