1 47 package com.lowagie.text.pdf; 48 49 import java.io.IOException ; 50 import java.io.OutputStream ; 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.Map ; 55 import java.util.StringTokenizer ; 56 57 import com.lowagie.text.DocWriter; 58 59 62 public class FdfWriter { 63 private static final byte[] HEADER_FDF = DocWriter.getISOBytes("%FDF-1.2\n%\u00e2\u00e3\u00cf\u00d3\n"); 64 HashMap fields = new HashMap (); 65 66 67 private String file; 68 69 70 public FdfWriter() { 71 } 72 73 77 public void writeTo(OutputStream os) throws IOException { 78 Wrt wrt = new Wrt(os, this); 79 wrt.writeTo(); 80 } 81 82 boolean setField(String field, PdfObject value) { 83 HashMap map = fields; 84 StringTokenizer tk = new StringTokenizer (field, "."); 85 if (!tk.hasMoreTokens()) 86 return false; 87 while (true) { 88 String s = tk.nextToken(); 89 Object obj = map.get(s); 90 if (tk.hasMoreTokens()) { 91 if (obj == null) { 92 obj = new HashMap (); 93 map.put(s, obj); 94 map = (HashMap )obj; 95 continue; 96 } 97 else if (obj instanceof HashMap ) 98 map = (HashMap )obj; 99 else 100 return false; 101 } 102 else { 103 if (!(obj instanceof HashMap )) { 104 map.put(s, value); 105 return true; 106 } 107 else 108 return false; 109 } 110 } 111 } 112 113 void iterateFields(HashMap values, HashMap map, String name) { 114 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 115 Map.Entry entry = (Map.Entry ) it.next(); 116 String s = (String ) entry.getKey(); 117 Object obj = entry.getValue(); 118 if (obj instanceof HashMap ) 119 iterateFields(values, (HashMap )obj, name + "." + s); 120 else 121 values.put((name + "." + s).substring(1), obj); 122 } 123 } 124 125 130 public boolean removeField(String field) { 131 HashMap map = fields; 132 StringTokenizer tk = new StringTokenizer (field, "."); 133 if (!tk.hasMoreTokens()) 134 return false; 135 ArrayList hist = new ArrayList (); 136 while (true) { 137 String s = tk.nextToken(); 138 Object obj = map.get(s); 139 if (obj == null) 140 return false; 141 hist.add(map); 142 hist.add(s); 143 if (tk.hasMoreTokens()) { 144 if (obj instanceof HashMap ) 145 map = (HashMap )obj; 146 else 147 return false; 148 } 149 else { 150 if (obj instanceof HashMap ) 151 return false; 152 else 153 break; 154 } 155 } 156 for (int k = hist.size() - 2; k >= 0; k -= 2) { 157 map = (HashMap )hist.get(k); 158 String s = (String )hist.get(k + 1); 159 map.remove(s); 160 if (!map.isEmpty()) 161 break; 162 } 163 return true; 164 } 165 166 170 public HashMap getFields() { 171 HashMap values = new HashMap (); 172 iterateFields(values, fields, ""); 173 return values; 174 } 175 176 180 public String getField(String field) { 181 HashMap map = fields; 182 StringTokenizer tk = new StringTokenizer (field, "."); 183 if (!tk.hasMoreTokens()) 184 return null; 185 while (true) { 186 String s = tk.nextToken(); 187 Object obj = map.get(s); 188 if (obj == null) 189 return null; 190 if (tk.hasMoreTokens()) { 191 if (obj instanceof HashMap ) 192 map = (HashMap )obj; 193 else 194 return null; 195 } 196 else { 197 if (obj instanceof HashMap ) 198 return null; 199 else { 200 if (((PdfObject)obj).isString()) 201 return ((PdfString)obj).toUnicodeString(); 202 else 203 return PdfName.decodeName(obj.toString()); 204 } 205 } 206 } 207 } 208 209 216 public boolean setFieldAsName(String field, String value) { 217 return setField(field, new PdfName(value)); 218 } 219 220 227 public boolean setFieldAsString(String field, String value) { 228 return setField(field, new PdfString(value, PdfObject.TEXT_UNICODE)); 229 } 230 231 234 public void setFields(FdfReader fdf) { 235 HashMap map = fdf.getFields(); 236 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 237 Map.Entry entry = (Map.Entry ) it.next(); 238 String key = (String ) entry.getKey(); 239 PdfDictionary dic = (PdfDictionary) entry.getValue(); 240 PdfObject v = dic.get(PdfName.V); 241 if (v != null) { 242 setField(key, v); 243 } 244 } 245 } 246 247 250 public void setFields(PdfReader pdf) { 251 setFields(pdf.getAcroFields()); 252 } 253 254 257 public void setFields(AcroFields af) { 258 for (Iterator it = af.getFields().entrySet().iterator(); it.hasNext();) { 259 Map.Entry entry = (Map.Entry )it.next(); 260 String fn = (String )entry.getKey(); 261 AcroFields.Item item = (AcroFields.Item)entry.getValue(); 262 PdfDictionary dic = (PdfDictionary)item.merged.get(0); 263 PdfObject v = PdfReader.getPdfObjectRelease(dic.get(PdfName.V)); 264 if (v == null) 265 continue; 266 PdfObject ft = PdfReader.getPdfObjectRelease(dic.get(PdfName.FT)); 267 if (ft == null || PdfName.SIG.equals(ft)) 268 continue; 269 setField(fn, v); 270 } 271 } 272 273 276 public String getFile() { 277 return this.file; 278 } 279 280 284 public void setFile(String file) { 285 this.file = file; 286 } 287 288 static class Wrt extends PdfWriter { 289 private FdfWriter fdf; 290 291 Wrt(OutputStream os, FdfWriter fdf) throws IOException { 292 super(new PdfDocument(), os); 293 this.fdf = fdf; 294 this.os.write(HEADER_FDF); 295 body = new PdfBody(this); 296 } 297 298 void writeTo() throws IOException { 299 PdfDictionary dic = new PdfDictionary(); 300 dic.put(PdfName.FIELDS, calculate(fdf.fields)); 301 if (fdf.file != null) 302 dic.put(PdfName.F, new PdfString(fdf.file, PdfObject.TEXT_UNICODE)); 303 PdfDictionary fd = new PdfDictionary(); 304 fd.put(PdfName.FDF, dic); 305 PdfIndirectReference ref = addToBody(fd).getIndirectReference(); 306 os.write(getISOBytes("trailer\n")); 307 PdfDictionary trailer = new PdfDictionary(); 308 trailer.put(PdfName.ROOT, ref); 309 trailer.toPdf(null, os); 310 os.write(getISOBytes("\n%%EOF\n")); 311 os.close(); 312 } 313 314 315 PdfArray calculate(HashMap map) throws IOException { 316 PdfArray ar = new PdfArray(); 317 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 318 Map.Entry entry = (Map.Entry ) it.next(); 319 String key = (String ) entry.getKey(); 320 Object v = entry.getValue(); 321 PdfDictionary dic = new PdfDictionary(); 322 dic.put(PdfName.T, new PdfString(key, PdfObject.TEXT_UNICODE)); 323 if (v instanceof HashMap ) { 324 dic.put(PdfName.KIDS, calculate((HashMap )v)); 325 } 326 else { 327 dic.put(PdfName.V, (PdfObject)v); 328 } 329 ar.add(dic); 330 } 331 return ar; 332 } 333 } 334 } 335 | Popular Tags |