1 31 package org.pdfbox.pdmodel.interactive.form; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSName; 37 import org.pdfbox.cos.COSString; 38 39 import org.pdfbox.pdmodel.PDDocument; 40 import org.pdfbox.pdmodel.PDResources; 41 42 import org.pdfbox.pdmodel.common.COSArrayList; 43 import org.pdfbox.pdmodel.common.COSObjectable; 44 import org.pdfbox.pdmodel.fdf.FDFDictionary; 45 import org.pdfbox.pdmodel.fdf.FDFDocument; 46 import org.pdfbox.pdmodel.fdf.FDFCatalog; 47 import org.pdfbox.pdmodel.fdf.FDFField; 48 49 import java.io.IOException ; 50 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import java.util.Map ; 56 57 63 public class PDAcroForm implements COSObjectable 64 { 65 private COSDictionary acroForm; 66 private PDDocument document; 67 68 private Map fieldCache; 69 70 75 public PDAcroForm( PDDocument doc ) 76 { 77 document = doc; 78 acroForm = new COSDictionary(); 79 COSArray fields = new COSArray(); 80 acroForm.setItem( COSName.getPDFName( "Fields" ), fields ); 81 } 82 83 89 public PDAcroForm( PDDocument doc, COSDictionary form ) 90 { 91 document = doc; 92 acroForm = form; 93 } 94 95 100 public PDDocument getDocument() 101 { 102 return document; 103 } 104 105 110 public COSDictionary getDictionary() 111 { 112 return acroForm; 113 } 114 115 123 public void importFDF( FDFDocument fdf ) throws IOException 124 { 125 List fields = fdf.getCatalog().getFDF().getFields(); 126 if( fields != null ) 127 { 128 for( int i=0; i<fields.size(); i++ ) 129 { 130 FDFField fdfField = (FDFField)fields.get( i ); 131 PDField docField = getField( fdfField.getPartialFieldName() ); 132 if( docField != null ) 133 { 134 docField.importFDF( fdfField ); 135 } 136 } 137 } 138 } 139 140 146 public FDFDocument exportFDF() throws IOException 147 { 148 FDFDocument fdf = new FDFDocument(); 149 FDFCatalog catalog = fdf.getCatalog(); 150 FDFDictionary fdfDict = new FDFDictionary(); 151 catalog.setFDF( fdfDict ); 152 153 List fdfFields = new ArrayList(); 154 List fields = getFields(); 155 Iterator fieldIter = fields.iterator(); 156 while( fieldIter.hasNext() ) 157 { 158 PDField docField = (PDField)fieldIter.next(); 159 addFieldAndChildren( docField, fdfFields ); 160 } 161 fdfDict.setID( document.getDocument().getDocumentID() ); 162 if( fdfFields.size() > 0 ) 163 { 164 fdfDict.setFields( fdfFields ); 165 } 166 return fdf; 167 } 168 169 private void addFieldAndChildren( PDField docField, List fdfFields ) throws IOException 170 { 171 Object fieldValue = docField.getValue(); 172 FDFField fdfField = new FDFField(); 173 fdfField.setPartialFieldName( docField.getPartialName() ); 174 fdfField.setValue( fieldValue ); 175 List kids = docField.getKids(); 176 List childFDFFields = new ArrayList(); 177 if( kids != null ) 178 { 179 180 for( int i=0; i<kids.size(); i++ ) 181 { 182 addFieldAndChildren( (PDField)kids.get( i ), childFDFFields ); 183 } 184 if( childFDFFields.size() > 0 ) 185 { 186 fdfField.setKids( childFDFFields ); 187 } 188 } 189 if( fieldValue != null || childFDFFields.size() > 0 ) 190 { 191 fdfFields.add( fdfField ); 192 } 193 } 194 195 202 public List getFields() throws IOException 203 { 204 List retval = null; 205 COSArray fields = 206 (COSArray) acroForm.getDictionaryObject( 207 COSName.getPDFName("Fields")); 208 209 if( fields != null ) 210 { 211 List actuals = new ArrayList(); 212 for (int i = 0; i < fields.size(); i++) 213 { 214 COSDictionary element = (COSDictionary) fields.getObject(i); 215 if (element != null) 216 { 217 PDField field = PDFieldFactory.createField( this, element ); 218 if( field != null ) 219 { 220 actuals.add(field); 221 } 222 } 223 } 224 retval = new COSArrayList( actuals, fields ); 225 } 226 return retval; 227 } 228 229 234 public void setFields( List fields ) 235 { 236 acroForm.setItem( "Fields", COSArrayList.converterToCOSArray( fields )); 237 } 238 239 248 public void setCacheFields( boolean cache ) throws IOException 249 { 250 if( cache ) 251 { 252 fieldCache = new HashMap (); 253 List fields = getFields(); 254 Iterator fieldIter = fields.iterator(); 255 while( fieldIter.hasNext() ) 256 { 257 PDField next = (PDField)fieldIter.next(); 258 fieldCache.put( next.getFullyQualifiedName(), next ); 259 } 260 } 261 else 262 { 263 fieldCache = null; 264 } 265 } 266 267 272 public boolean isCachingFields() 273 { 274 return fieldCache != null; 275 } 276 277 286 public PDField getField( String name ) throws IOException 287 { 288 PDField retval = null; 289 if( fieldCache != null ) 290 { 291 retval = (PDField)fieldCache.get( name ); 292 } 293 else 294 { 295 String [] nameSubSection = name.split( "\\." ); 296 COSArray fields = 297 (COSArray) acroForm.getDictionaryObject( 298 COSName.getPDFName("Fields")); 299 300 for (int i = 0; i < fields.size() && retval == null; i++) 301 { 302 COSDictionary element = (COSDictionary) fields.getObject(i); 303 if( element != null ) 304 { 305 COSString fieldName = 306 (COSString)element.getDictionaryObject( COSName.getPDFName( "T" ) ); 307 if( fieldName.getString().equals( name ) || 308 fieldName.getString().equals( nameSubSection[0] ) ) 309 { 310 PDField root = PDFieldFactory.createField( this, element ); 311 312 if( nameSubSection.length > 1 ) 313 { 314 PDField kid = root.findKid( nameSubSection, 1 ); 315 if( kid != null ) 316 { 317 retval = kid; 318 } 319 else 320 { 321 retval = root; 322 } 323 } 324 else 325 { 326 retval = root; 327 } 328 } 329 } 330 } 331 } 332 return retval; 333 } 334 335 340 public PDResources getDefaultResources() 341 { 342 PDResources retval = null; 343 COSDictionary dr = (COSDictionary)acroForm.getDictionaryObject( COSName.getPDFName( "DR" ) ); 344 if( dr != null ) 345 { 346 retval = new PDResources( dr ); 347 } 348 return retval; 349 } 350 351 356 public void setDefaultResources( PDResources dr ) 357 { 358 COSDictionary drDict = null; 359 if( dr != null ) 360 { 361 drDict = dr.getCOSDictionary(); 362 } 363 acroForm.setItem( COSName.getPDFName( "DR" ), drDict ); 364 } 365 366 369 public COSBase getCOSObject() 370 { 371 return acroForm; 372 } 373 374 379 public PDXFA getXFA() 380 { 381 PDXFA xfa = null; 382 COSBase base = acroForm.getDictionaryObject( "XFA" ); 383 if( base != null ) 384 { 385 xfa = new PDXFA( base ); 386 } 387 return xfa; 388 } 389 390 395 public void setXFA( PDXFA xfa ) 396 { 397 acroForm.setItem( "XFA", xfa ); 398 } 399 } | Popular Tags |