1 31 package org.pdfbox.pdmodel.interactive.form; 32 33 import org.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions; 34 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; 35 36 import org.pdfbox.pdmodel.common.COSArrayList; 37 import org.pdfbox.pdmodel.common.COSObjectable; 38 39 import org.pdfbox.cos.COSArray; 40 import org.pdfbox.cos.COSBase; 41 import org.pdfbox.cos.COSDictionary; 42 import org.pdfbox.cos.COSInteger; 43 import org.pdfbox.cos.COSName; 44 45 import org.pdfbox.pdmodel.common.PDTextStream; 46 47 import org.pdfbox.pdmodel.fdf.FDFField; 48 import org.pdfbox.util.BitFlagHelper; 49 50 import java.io.IOException ; 51 52 import java.util.ArrayList ; 53 import java.util.List ; 54 55 62 public abstract class PDField implements COSObjectable 63 { 64 67 public static final int FLAG_READ_ONLY = 1; 68 71 public static final int FLAG_REQUIRED = 1 << 1; 72 75 public static final int FLAG_NO_EXPORT = 1 << 2; 76 77 78 private PDAcroForm acroForm; 79 80 private COSDictionary dictionary; 81 82 87 public PDField( PDAcroForm theAcroForm ) 88 { 89 acroForm = theAcroForm; 90 dictionary = new COSDictionary(); 91 } 93 94 95 102 public PDField(PDAcroForm theAcroForm, COSDictionary field) 103 { 104 acroForm = theAcroForm; 105 dictionary = field; 106 } 107 108 113 public String getPartialName() 114 { 115 return getDictionary().getString( "T" ); 116 } 117 118 123 public void setPartialName( String name ) 124 { 125 getDictionary().setString( "T", name ); 126 } 127 128 136 public String getFullyQualifiedName() throws IOException 137 { 138 PDField parent = getParent(); 139 String parentName = null; 140 if( parent != null ) 141 { 142 parentName = parent.getFullyQualifiedName(); 143 } 144 String finalName = getPartialName(); 145 if( parentName != null ) 146 { 147 finalName = parentName + "." + finalName; 148 } 149 return finalName; 150 } 151 152 162 public String getFieldType() 163 { 164 return getDictionary().getNameAsString( "FT" ); 165 } 166 167 176 public String findFieldType() 177 { 178 return findFieldType( getDictionary() ); 179 } 180 181 private String findFieldType( COSDictionary dic ) 182 { 183 String retval = dic.getNameAsString( "FT" ); 184 if( retval == null ) 185 { 186 COSDictionary parent = (COSDictionary)dic.getDictionaryObject( "Parent", "P" ); 187 if( parent != null ) 188 { 189 retval = findFieldType( parent ); 190 } 191 } 192 return retval; 193 194 } 195 196 197 204 public abstract void setValue(String value) throws IOException ; 205 206 213 public abstract String getValue() throws IOException ; 214 215 220 public void setReadonly(boolean readonly) 221 { 222 BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_READ_ONLY, readonly ); 223 } 224 225 229 public boolean isReadonly() 230 { 231 return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_READ_ONLY ); 232 } 233 234 239 public void setRequired(boolean required) 240 { 241 BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_REQUIRED, required ); 242 } 243 244 248 public boolean isRequired() 249 { 250 return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_REQUIRED ); 251 } 252 253 258 public void setNoExport(boolean noExport) 259 { 260 BitFlagHelper.setFlag( getDictionary(), "Ff", FLAG_NO_EXPORT, noExport ); 261 } 262 263 267 public boolean isNoExport() 268 { 269 return BitFlagHelper.getFlag( getDictionary(), "Ff", FLAG_NO_EXPORT ); 270 } 271 272 277 public int getFieldFlags() 278 { 279 int retval = 0; 280 COSInteger ff = (COSInteger)getDictionary().getDictionaryObject( COSName.getPDFName( "Ff" ) ); 281 if( ff != null ) 282 { 283 retval = ff.intValue(); 284 } 285 return retval; 286 } 287 288 293 public void setFieldFlags( int flags ) 294 { 295 COSInteger ff = new COSInteger( flags ); 296 getDictionary().setItem( COSName.getPDFName( "Ff" ), ff ); 297 } 298 299 306 public void importFDF( FDFField fdfField ) throws IOException 307 { 308 Object fieldValue = fdfField.getValue(); 309 int fieldFlags = getFieldFlags(); 310 311 if( fieldValue != null ) 312 { 313 if( fieldValue instanceof String ) 314 { 315 setValue( (String )fieldValue ); 316 } 317 else if( fieldValue instanceof PDTextStream ) 318 { 319 setValue( ((PDTextStream)fieldValue).getAsString() ); 320 } 321 else 322 { 323 throw new IOException ( "Unknown field type:" + fieldValue.getClass().getName() ); 324 } 325 } 326 Integer ff = fdfField.getFieldFlags(); 327 if( ff != null ) 328 { 329 setFieldFlags( ff.intValue() ); 330 } 331 else 332 { 333 Integer setFf = fdfField.getSetFieldFlags(); 335 336 if( setFf != null ) 337 { 338 int setFfInt = setFf.intValue(); 339 fieldFlags = fieldFlags | setFfInt; 340 setFieldFlags( fieldFlags ); 341 } 342 343 Integer clrFf = fdfField.getClearFieldFlags(); 344 if( clrFf != null ) 345 { 346 int clrFfValue = clrFf.intValue(); 355 clrFfValue ^= 0xFFFFFFFF; 356 fieldFlags = fieldFlags & clrFfValue; 357 setFieldFlags( fieldFlags ); 358 } 359 } 360 361 PDAnnotationWidget widget = getWidget(); 362 if( widget != null ) 363 { 364 int annotFlags = widget.getAnnotationFlags(); 365 Integer f = fdfField.getWidgetFieldFlags(); 366 if( f != null && widget != null ) 367 { 368 widget.setAnnotationFlags( f.intValue() ); 369 } 370 else 371 { 372 Integer setF = fdfField.getSetWidgetFieldFlags(); 374 if( setF != null ) 375 { 376 annotFlags = annotFlags | setF.intValue(); 377 widget.setAnnotationFlags( annotFlags ); 378 } 379 380 Integer clrF = fdfField.getClearWidgetFieldFlags(); 381 if( clrF != null ) 382 { 383 int clrFValue = clrF.intValue(); 392 clrFValue ^= 0xFFFFFFFFL; 393 annotFlags = annotFlags & clrFValue; 394 widget.setAnnotationFlags( annotFlags ); 395 } 396 } 397 } 398 List fdfKids = fdfField.getKids(); 399 List pdKids = getKids(); 400 for( int i=0; fdfKids != null && i<fdfKids.size(); i++ ) 401 { 402 FDFField fdfChild = (FDFField)fdfKids.get( i ); 403 String fdfName = fdfChild.getPartialFieldName(); 404 for( int j=0; j<pdKids.size(); j++ ) 405 { 406 Object pdChildObj = pdKids.get( j ); 407 if( pdChildObj instanceof PDField ) 408 { 409 PDField pdChild = (PDField)pdChildObj; 410 if( fdfName != null && fdfName.equals( pdChild.getPartialName() ) ) 411 { 412 pdChild.importFDF( fdfChild ); 413 } 414 } 415 } 416 } 417 } 418 419 429 public PDAnnotationWidget getWidget() throws IOException 430 { 431 PDAnnotationWidget retval = null; 432 List kids = getKids(); 433 if( kids == null ) 434 { 435 retval = new PDAnnotationWidget( getDictionary() ); 436 } 437 else if( kids.size() > 0 ) 438 { 439 Object firstKid = kids.get( 0 ); 440 if( firstKid instanceof PDAnnotationWidget ) 441 { 442 retval = (PDAnnotationWidget)firstKid; 443 } 444 else 445 { 446 retval = ((PDField)firstKid).getWidget(); 447 } 448 } 449 else 450 { 451 retval = null; 452 } 453 return retval; 454 } 455 456 463 public PDField getParent() throws IOException 464 { 465 PDField parent = null; 466 COSDictionary parentDic = (COSDictionary)getDictionary().getDictionaryObject( "Parent" ); 467 if( parentDic != null ) 468 { 469 parent = PDFieldFactory.createField( getAcroForm(), parentDic ); 470 } 471 return parent; 472 } 473 474 479 public void setParent( PDField parent ) 480 { 481 getDictionary().setItem( "Parent", parent ); 482 } 483 484 495 public PDField findKid( String [] name, int nameIndex ) throws IOException 496 { 497 PDField retval = null; 498 COSArray kids = (COSArray)getDictionary().getDictionaryObject( COSName.KIDS ); 499 if( kids != null ) 500 { 501 for (int i = 0; retval == null && i < kids.size(); i++) 502 { 503 COSDictionary kidDictionary = (COSDictionary)kids.getObject(i); 504 if( name[nameIndex].equals( kidDictionary.getString( "T" ) ) ) 505 { 506 retval = PDFieldFactory.createField( acroForm, kidDictionary ); 507 if( name.length > nameIndex+1 ) 508 { 509 retval = retval.findKid( name, nameIndex+1 ); 510 } 511 } 512 } 513 } 514 return retval; 515 } 516 517 525 public List getKids() throws IOException 526 { 527 List retval = null; 528 COSArray kids = (COSArray)getDictionary().getDictionaryObject(COSName.KIDS); 529 if( kids != null ) 530 { 531 List kidsList = new ArrayList(); 532 for (int i = 0; i < kids.size(); i++) 533 { 534 COSDictionary kidDictionary = (COSDictionary)kids.getObject(i); 535 COSDictionary parent = (COSDictionary)kidDictionary.getDictionaryObject( "Parent" ); 536 if( kidDictionary.getDictionaryObject( "FT" ) != null || 537 (parent != null && parent.getDictionaryObject( "FT" ) != null ) ) 538 { 539 kidsList.add( PDFieldFactory.createField( acroForm, kidDictionary )); 540 } 541 else if( "Widget".equals( kidDictionary.getNameAsString( "Subtype" ) ) ) 542 { 543 kidsList.add( new PDAnnotationWidget( kidDictionary ) ); 544 } 545 else 546 { 547 kidsList.add( PDFieldFactory.createField( acroForm, kidDictionary )); 549 } 550 } 551 retval = new COSArrayList( kidsList, kids ); 552 } 553 return retval; 554 } 555 556 561 public void setKids( List kids ) 562 { 563 COSArray kidsArray = COSArrayList.converterToCOSArray( kids ); 564 getDictionary().setItem( COSName.KIDS, kidsArray ); 565 } 566 567 572 public String toString() 573 { 574 return "" + getDictionary().getDictionaryObject( COSName.getPDFName( "V" ) ); 575 } 576 577 582 public PDAcroForm getAcroForm() 583 { 584 return acroForm; 585 } 586 587 592 public void setAcroForm(PDAcroForm value) 593 { 594 acroForm = value; 595 } 596 597 602 public COSDictionary getDictionary() 603 { 604 return dictionary; 605 } 606 607 612 public COSBase getCOSObject() 613 { 614 return dictionary; 615 } 616 617 623 public PDFormFieldAdditionalActions getActions() 624 { 625 COSDictionary aa = (COSDictionary)dictionary.getDictionaryObject( "AA" ); 626 PDFormFieldAdditionalActions retval = null; 627 if( aa != null ) 628 { 629 retval = new PDFormFieldAdditionalActions( aa ); 630 } 631 return retval; 632 } 633 634 639 public void setActions( PDFormFieldAdditionalActions actions ) 640 { 641 dictionary.setItem( "AA", actions ); 642 } 643 } | Popular Tags |