1 47 48 package com.lowagie.text.pdf; 49 import java.util.ArrayList ; 50 import java.util.Iterator ; 51 52 import com.lowagie.text.Rectangle; 53 54 58 public class PdfFormField extends PdfAnnotation { 59 60 public static final int FF_READ_ONLY = 1; 61 public static final int FF_REQUIRED = 2; 62 public static final int FF_NO_EXPORT = 4; 63 public static final int FF_NO_TOGGLE_TO_OFF = 16384; 64 public static final int FF_RADIO = 32768; 65 public static final int FF_PUSHBUTTON = 65536; 66 public static final int FF_MULTILINE = 4096; 67 public static final int FF_PASSWORD = 8192; 68 public static final int FF_COMBO = 131072; 69 public static final int FF_EDIT = 262144; 70 public static final int FF_FILESELECT = 1048576; 71 public static final int FF_MULTISELECT = 2097152; 72 public static final int FF_DONOTSPELLCHECK = 4194304; 73 public static final int FF_DONOTSCROLL = 8388608; 74 public static final int FF_COMB = 16777216; 75 public static final int FF_RADIOSINUNISON = 1 << 25; 76 public static final int Q_LEFT = 0; 77 public static final int Q_CENTER = 1; 78 public static final int Q_RIGHT = 2; 79 public static final int MK_NO_ICON = 0; 80 public static final int MK_NO_CAPTION = 1; 81 public static final int MK_CAPTION_BELOW = 2; 82 public static final int MK_CAPTION_ABOVE = 3; 83 public static final int MK_CAPTION_RIGHT = 4; 84 public static final int MK_CAPTION_LEFT = 5; 85 public static final int MK_CAPTION_OVERLAID = 6; 86 public static final PdfName IF_SCALE_ALWAYS = PdfName.A; 87 public static final PdfName IF_SCALE_BIGGER = PdfName.B; 88 public static final PdfName IF_SCALE_SMALLER = PdfName.S; 89 public static final PdfName IF_SCALE_NEVER = PdfName.N; 90 public static final PdfName IF_SCALE_ANAMORPHIC = PdfName.A; 91 public static final PdfName IF_SCALE_PROPORTIONAL = PdfName.P; 92 public static final boolean MULTILINE = true; 93 public static final boolean SINGLELINE = false; 94 public static final boolean PLAINTEXT = false; 95 public static final boolean PASSWORD = true; 96 static PdfName mergeTarget[] = {PdfName.FONT, PdfName.XOBJECT, PdfName.COLORSPACE, PdfName.PATTERN}; 97 98 99 protected PdfFormField parent; 100 101 protected ArrayList kids; 102 103 106 107 public PdfFormField(PdfWriter writer, float llx, float lly, float urx, float ury, PdfAction action) { 108 super(writer, llx, lly, urx, ury, action); 109 put(PdfName.TYPE, PdfName.ANNOT); 110 put(PdfName.SUBTYPE, PdfName.WIDGET); 111 annotation = true; 112 } 113 114 115 protected PdfFormField(PdfWriter writer) { 116 super(writer, null); 117 form = true; 118 annotation = false; 119 } 120 121 public void setWidget(Rectangle rect, PdfName highlight) { 122 put(PdfName.TYPE, PdfName.ANNOT); 123 put(PdfName.SUBTYPE, PdfName.WIDGET); 124 put(PdfName.RECT, new PdfRectangle(rect)); 125 annotation = true; 126 if (highlight != null && !highlight.equals(HIGHLIGHT_INVERT)) 127 put(PdfName.H, highlight); 128 } 129 130 public static PdfFormField createEmpty(PdfWriter writer) { 131 PdfFormField field = new PdfFormField(writer); 132 return field; 133 } 134 135 public void setButton(int flags) { 136 put(PdfName.FT, PdfName.BTN); 137 if (flags != 0) 138 put(PdfName.FF, new PdfNumber(flags)); 139 } 140 141 protected static PdfFormField createButton(PdfWriter writer, int flags) { 142 PdfFormField field = new PdfFormField(writer); 143 field.setButton(flags); 144 return field; 145 } 146 147 public static PdfFormField createPushButton(PdfWriter writer) { 148 return createButton(writer, FF_PUSHBUTTON); 149 } 150 151 public static PdfFormField createCheckBox(PdfWriter writer) { 152 return createButton(writer, 0); 153 } 154 155 public static PdfFormField createRadioButton(PdfWriter writer, boolean noToggleToOff) { 156 return createButton(writer, FF_RADIO + (noToggleToOff ? FF_NO_TOGGLE_TO_OFF : 0)); 157 } 158 159 public static PdfFormField createTextField(PdfWriter writer, boolean multiline, boolean password, int maxLen) { 160 PdfFormField field = new PdfFormField(writer); 161 field.put(PdfName.FT, PdfName.TX); 162 int flags = (multiline ? FF_MULTILINE : 0); 163 flags += (password ? FF_PASSWORD : 0); 164 field.put(PdfName.FF, new PdfNumber(flags)); 165 if (maxLen > 0) 166 field.put(PdfName.MAXLEN, new PdfNumber(maxLen)); 167 return field; 168 } 169 170 protected static PdfFormField createChoice(PdfWriter writer, int flags, PdfArray options, int topIndex) { 171 PdfFormField field = new PdfFormField(writer); 172 field.put(PdfName.FT, PdfName.CH); 173 field.put(PdfName.FF, new PdfNumber(flags)); 174 field.put(PdfName.OPT, options); 175 if (topIndex > 0) 176 field.put(PdfName.TI, new PdfNumber(topIndex)); 177 return field; 178 } 179 180 public static PdfFormField createList(PdfWriter writer, String options[], int topIndex) { 181 return createChoice(writer, 0, processOptions(options), topIndex); 182 } 183 184 public static PdfFormField createList(PdfWriter writer, String options[][], int topIndex) { 185 return createChoice(writer, 0, processOptions(options), topIndex); 186 } 187 188 public static PdfFormField createCombo(PdfWriter writer, boolean edit, String options[], int topIndex) { 189 return createChoice(writer, FF_COMBO + (edit ? FF_EDIT : 0), processOptions(options), topIndex); 190 } 191 192 public static PdfFormField createCombo(PdfWriter writer, boolean edit, String options[][], int topIndex) { 193 return createChoice(writer, FF_COMBO + (edit ? FF_EDIT : 0), processOptions(options), topIndex); 194 } 195 196 protected static PdfArray processOptions(String options[]) { 197 PdfArray array = new PdfArray(); 198 for (int k = 0; k < options.length; ++k) { 199 array.add(new PdfString(options[k], PdfObject.TEXT_UNICODE)); 200 } 201 return array; 202 } 203 204 protected static PdfArray processOptions(String options[][]) { 205 PdfArray array = new PdfArray(); 206 for (int k = 0; k < options.length; ++k) { 207 String subOption[] = options[k]; 208 PdfArray ar2 = new PdfArray(new PdfString(subOption[0], PdfObject.TEXT_UNICODE)); 209 ar2.add(new PdfString(subOption[1], PdfObject.TEXT_UNICODE)); 210 array.add(ar2); 211 } 212 return array; 213 } 214 215 public static PdfFormField createSignature(PdfWriter writer) { 216 PdfFormField field = new PdfFormField(writer); 217 field.put(PdfName.FT, PdfName.SIG); 218 return field; 219 } 220 221 224 public PdfFormField getParent() { 225 return parent; 226 } 227 228 public void addKid(PdfFormField field) { 229 field.parent = this; 230 if (kids == null) 231 kids = new ArrayList (); 232 kids.add(field); 233 } 234 235 public ArrayList getKids() { 236 return kids; 237 } 238 239 public int setFieldFlags(int flags) { 240 PdfNumber obj = (PdfNumber)get(PdfName.FF); 241 int old; 242 if (obj == null) 243 old = 0; 244 else 245 old = obj.intValue(); 246 int v = old | flags; 247 put(PdfName.FF, new PdfNumber(v)); 248 return old; 249 } 250 251 public void setValueAsString(String s) { 252 put(PdfName.V, new PdfString(s, PdfObject.TEXT_UNICODE)); 253 } 254 255 public void setValueAsName(String s) { 256 put(PdfName.V, new PdfName(s)); 257 } 258 259 public void setValue(PdfSignature sig) { 260 put(PdfName.V, sig); 261 } 262 263 public void setDefaultValueAsString(String s) { 264 put(PdfName.DV, new PdfString(s, PdfObject.TEXT_UNICODE)); 265 } 266 267 public void setDefaultValueAsName(String s) { 268 put(PdfName.DV, new PdfName(s)); 269 } 270 271 public void setFieldName(String s) { 272 if (s != null) 273 put(PdfName.T, new PdfString(s, PdfObject.TEXT_UNICODE)); 274 } 275 276 public void setUserName(String s) { 277 put(PdfName.TU, new PdfString(s, PdfObject.TEXT_UNICODE)); 278 } 279 280 public void setMappingName(String s) { 281 put(PdfName.TM, new PdfString(s, PdfObject.TEXT_UNICODE)); 282 } 283 284 public void setQuadding(int v) { 285 put(PdfName.Q, new PdfNumber(v)); 286 } 287 288 static void mergeResources(PdfDictionary result, PdfDictionary source, PdfStamperImp writer) { 289 PdfDictionary dic = null; 290 PdfDictionary res = null; 291 PdfName target = null; 292 for (int k = 0; k < mergeTarget.length; ++k) { 293 target = mergeTarget[k]; 294 PdfDictionary pdfDict = (PdfDictionary)PdfReader.getPdfObject(source.get(target)); 295 if ((dic = pdfDict) != null) { 296 if ((res = (PdfDictionary)PdfReader.getPdfObject(result.get(target), result)) == null) { 297 res = new PdfDictionary(); 298 } 299 res.mergeDifferent(dic); 300 result.put(target, res); 301 if (writer != null) 302 writer.markUsed(res); 303 } 304 } 305 } 306 307 static void mergeResources(PdfDictionary result, PdfDictionary source) { 308 mergeResources(result, source, null); 309 } 310 311 public void setUsed() { 312 used = true; 313 if (parent != null) 314 put(PdfName.PARENT, parent.getIndirectReference()); 315 if (kids != null) { 316 PdfArray array = new PdfArray(); 317 for (int k = 0; k < kids.size(); ++k) 318 array.add(((PdfFormField)kids.get(k)).getIndirectReference()); 319 put(PdfName.KIDS, array); 320 } 321 if (templates == null) 322 return; 323 PdfDictionary dic = new PdfDictionary(); 324 for (Iterator it = templates.keySet().iterator(); it.hasNext();) { 325 PdfTemplate template = (PdfTemplate)it.next(); 326 mergeResources(dic, (PdfDictionary)template.getResources()); 327 } 328 put(PdfName.DR, dic); 329 } 330 331 public static PdfAnnotation shallowDuplicate(PdfAnnotation annot) { 332 PdfAnnotation dup; 333 if (annot.isForm()) { 334 dup = new PdfFormField(annot.writer); 335 PdfFormField dupField = (PdfFormField)dup; 336 PdfFormField srcField = (PdfFormField)annot; 337 dupField.parent = srcField.parent; 338 dupField.kids = srcField.kids; 339 } 340 else 341 dup = new PdfAnnotation(annot.writer, null); 342 dup.merge(annot); 343 dup.form = annot.form; 344 dup.annotation = annot.annotation; 345 dup.templates = annot.templates; 346 return dup; 347 } 348 } 349 | Popular Tags |