1 52 53 package com.lowagie.text.pdf; 54 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.Iterator ; 58 59 64 65 public class PRAcroForm extends PdfDictionary { 66 67 70 public static class FieldInformation { 71 String name; 72 PdfDictionary info; 73 PRIndirectReference ref; 74 75 FieldInformation(String name, PdfDictionary info, PRIndirectReference ref) { 76 this.name = name; this.info = info; this.ref = ref; 77 } 78 public String getName() { return name; } 79 public PdfDictionary getInfo() { return info; } 80 public PRIndirectReference getRef() { return ref; } 81 }; 82 ArrayList fields; 83 ArrayList stack; 84 HashMap fieldByName; 85 PdfReader reader; 86 87 91 public PRAcroForm(PdfReader reader) { 92 this.reader = reader; 93 fields = new ArrayList (); 94 fieldByName = new HashMap (); 95 stack = new ArrayList (); 96 } 97 101 public int size() { 102 return fields.size(); 103 } 104 105 public ArrayList getFields() { 106 return fields; 107 } 108 109 public FieldInformation getField(String name) { 110 return (FieldInformation)fieldByName.get(name); 111 } 112 113 118 public PRIndirectReference getRefByName(String name) { 119 FieldInformation fi = (FieldInformation)fieldByName.get(name); 120 if (fi == null) return null; 121 return fi.getRef(); 122 } 123 127 public void readAcroForm(PdfDictionary root) { 128 if (root == null) 129 return; 130 hashMap = root.hashMap; 131 pushAttrib(root); 132 PdfArray fieldlist = (PdfArray)PdfReader.getPdfObjectRelease(root.get(PdfName.FIELDS)); 133 iterateFields(fieldlist, null, null); 134 } 135 136 142 protected void iterateFields(PdfArray fieldlist, PRIndirectReference fieldDict, String title) { 143 for (Iterator it = fieldlist.getArrayList().iterator(); it.hasNext();) { 144 PRIndirectReference ref = (PRIndirectReference)it.next(); 145 PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObjectRelease(ref); 146 147 PRIndirectReference myFieldDict = fieldDict; 149 String myTitle = title; 150 PdfString tField = (PdfString)dict.get(PdfName.T); 151 boolean isFieldDict = tField != null; 152 153 if (isFieldDict) { 154 myFieldDict = ref; 155 if (title == null) myTitle = tField.toString(); 156 else myTitle = title + '.' + tField.toString(); 157 } 158 159 PdfArray kids = (PdfArray)dict.get(PdfName.KIDS); 160 if (kids != null) { 161 pushAttrib(dict); 162 iterateFields(kids, myFieldDict, myTitle); 163 stack.remove(stack.size() - 1); } 165 else { if (myFieldDict != null) { 167 PdfDictionary mergedDict = (PdfDictionary)stack.get(stack.size() - 1); 168 if (isFieldDict) 169 mergedDict = mergeAttrib(mergedDict, dict); 170 171 mergedDict.put(PdfName.T, new PdfString(myTitle)); 172 FieldInformation fi = new FieldInformation(myTitle, mergedDict, myFieldDict); 173 fields.add(fi); 174 fieldByName.put(myTitle, fi); 175 } 176 } 177 } 178 } 179 185 protected PdfDictionary mergeAttrib(PdfDictionary parent, PdfDictionary child) { 186 PdfDictionary targ = new PdfDictionary(); 187 if (parent != null) targ.putAll(parent); 188 189 for (Iterator it = child.getKeys().iterator(); it.hasNext();) { 190 PdfName key = (PdfName) it.next(); 191 if (key.equals(PdfName.DR) || key.equals(PdfName.DA) || 192 key.equals(PdfName.Q) || key.equals(PdfName.FF) || 193 key.equals(PdfName.DV) || key.equals(PdfName.V) 194 || key.equals(PdfName.FT) 195 || key.equals(PdfName.F)) { 196 targ.put(key,child.get(key)); 197 } 198 } 199 return targ; 200 } 201 204 protected void pushAttrib(PdfDictionary dict) { 205 PdfDictionary dic = null; 206 if (!stack.isEmpty()) { 207 dic = (PdfDictionary)stack.get(stack.size() - 1); 208 } 209 dic = mergeAttrib(dic, dict); 210 stack.add(dic); 211 } 212 } 213 | Popular Tags |