1 31 package org.pdfbox.pdmodel.fdf; 32 33 import java.io.IOException ; 34 import java.io.Writer ; 35 36 import java.util.ArrayList ; 37 import java.util.List ; 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 import org.pdfbox.cos.COSNumber; 45 import org.pdfbox.cos.COSStream; 46 import org.pdfbox.cos.COSString; 47 48 import org.pdfbox.pdmodel.common.COSObjectable; 49 import org.pdfbox.pdmodel.common.COSArrayList; 50 import org.pdfbox.pdmodel.common.PDTextStream; 51 52 import org.pdfbox.pdmodel.interactive.action.PDActionFactory; 53 import org.pdfbox.pdmodel.interactive.action.PDAdditionalActions; 54 import org.pdfbox.pdmodel.interactive.action.type.PDAction; 55 56 import org.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary; 57 58 import org.pdfbox.util.XMLUtil; 59 60 import org.w3c.dom.Element ; 61 import org.w3c.dom.Node ; 62 import org.w3c.dom.NodeList ; 63 64 70 public class FDFField implements COSObjectable 71 { 72 private COSDictionary field; 73 74 77 public FDFField() 78 { 79 field = new COSDictionary(); 80 } 81 82 87 public FDFField( COSDictionary f ) 88 { 89 field = f; 90 } 91 92 98 public FDFField( Element fieldXML ) throws IOException 99 { 100 this(); 101 this.setPartialFieldName( fieldXML.getAttribute( "name" ) ); 102 NodeList nodeList = fieldXML.getChildNodes(); 103 List kids = new ArrayList (); 104 for( int i=0; i<nodeList.getLength(); i++ ) 105 { 106 Node node = nodeList.item( i ); 107 if( node instanceof Element ) 108 { 109 Element child = (Element )node; 110 if( child.getTagName().equals( "value" ) ) 111 { 112 setValue( XMLUtil.getNodeValue( child ) ); 113 } 114 else if( child.getTagName().equals( "value-richtext" ) ) 115 { 116 setRichText( new PDTextStream( XMLUtil.getNodeValue( child ) ) ); 117 } 118 else if( child.getTagName().equals( "field" ) ) 119 { 120 kids.add( new FDFField( child ) ); 121 } 122 } 123 } 124 if( kids.size() > 0 ) 125 { 126 setKids( kids ); 127 } 128 129 } 130 131 138 public void writeXML( Writer output ) throws IOException 139 { 140 output.write( "<field name=\"" + getPartialFieldName() + "\">\n"); 141 Object value = getValue(); 142 if( value != null ) 143 { 144 output.write( "<value>" + value + "</value>\n" ); 145 } 146 PDTextStream rt = getRichText(); 147 if( rt != null ) 148 { 149 output.write( "<value-richtext>" + rt.getAsString() + "</value-richtext>\n" ); 150 } 151 List kids = getKids(); 152 if( kids != null ) 153 { 154 for( int i=0; i<kids.size(); i++ ) 155 { 156 ((FDFField)kids.get( i ) ).writeXML( output ); 157 } 158 } 159 output.write( "</field>\n"); 160 } 161 162 167 public COSBase getCOSObject() 168 { 169 return field; 170 } 171 172 177 public COSDictionary getCOSDictionary() 178 { 179 return field; 180 } 181 182 188 public List getKids() 189 { 190 COSArray kids = (COSArray)field.getDictionaryObject( "Kids" ); 191 List retval = null; 192 if( kids != null ) 193 { 194 List actuals = new ArrayList (); 195 for( int i=0; i<kids.size(); i++ ) 196 { 197 actuals.add( new FDFField( (COSDictionary)kids.getObject( i ) ) ); 198 } 199 retval = new COSArrayList( actuals, kids ); 200 } 201 return retval; 202 } 203 204 209 public void setKids( List kids ) 210 { 211 field.setItem( "Kids", COSArrayList.converterToCOSArray( kids ) ); 212 } 213 214 223 public String getPartialFieldName() 224 { 225 return field.getString( "T" ); 226 } 227 228 233 public void setPartialFieldName( String partial ) 234 { 235 field.setString( "T", partial ); 236 } 237 238 248 public Object getValue() throws IOException 249 { 250 Object retval = null; 251 COSBase value = field.getDictionaryObject( "V" ); 252 if( value instanceof COSName ) 253 { 254 retval = ((COSName)value).getName(); 255 } 256 else if( value instanceof COSArray ) 257 { 258 retval = COSArrayList.convertCOSStringCOSArrayToList( (COSArray)value ); 259 } 260 else if( value instanceof COSString || value instanceof COSStream ) 261 { 262 retval = PDTextStream.createTextStream( value ); 263 } 264 else if( value == null ) 265 { 266 } 268 else 269 { 270 throw new IOException ( "Error:Unknown type for field import" + value ); 271 } 272 return retval; 273 } 274 275 283 public void setValue( Object value ) throws IOException 284 { 285 COSBase cos = null; 286 if( value instanceof List ) 287 { 288 cos = COSArrayList.convertStringListToCOSStringCOSArray( (List )value ); 289 } 290 else if( value instanceof String ) 291 { 292 cos = COSName.getPDFName( (String )value ); 293 } 294 else if( value instanceof COSObjectable ) 295 { 296 cos = ((COSObjectable)value).getCOSObject(); 297 } 298 else if( value == null ) 299 { 300 } 302 else 303 { 304 throw new IOException ( "Error:Unknown type for field import" + value ); 305 } 306 field.setItem( "V", cos ); 307 } 308 309 315 public Integer getFieldFlags() 316 { 317 Integer retval = null; 318 COSNumber ff = (COSNumber)field.getDictionaryObject( "Ff" ); 319 if( ff != null ) 320 { 321 retval = new Integer ( ff.intValue() ); 322 } 323 return retval; 324 } 325 326 332 public void setFieldFlags( Integer ff ) 333 { 334 COSInteger value = null; 335 if( ff != null ) 336 { 337 value = new COSInteger( ff.intValue() ); 338 } 339 field.setItem( "Ff", value ); 340 } 341 342 348 public void setFieldFlags( int ff ) 349 { 350 field.setItem( "Ff", new COSInteger( ff ) ); 351 } 352 353 359 public Integer getSetFieldFlags() 360 { 361 Integer retval = null; 362 COSNumber ff = (COSNumber)field.getDictionaryObject( "SetFf" ); 363 if( ff != null ) 364 { 365 retval = new Integer ( ff.intValue() ); 366 } 367 return retval; 368 } 369 370 376 public void setSetFieldFlags( Integer ff ) 377 { 378 COSInteger value = null; 379 if( ff != null ) 380 { 381 value = new COSInteger( ff.intValue() ); 382 } 383 field.setItem( "SetFf", value ); 384 } 385 386 392 public void setSetFieldFlags( int ff ) 393 { 394 field.setItem( "SetFf", new COSInteger( ff ) ); 395 } 396 397 403 public Integer getClearFieldFlags() 404 { 405 Integer retval = null; 406 COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrFf" ); 407 if( ff != null ) 408 { 409 retval = new Integer ( ff.intValue() ); 410 } 411 return retval; 412 } 413 414 420 public void setClearFieldFlags( Integer ff ) 421 { 422 COSInteger value = null; 423 if( ff != null ) 424 { 425 value = new COSInteger( ff.intValue() ); 426 } 427 field.setItem( "ClrFf", value ); 428 } 429 430 436 public void setClearFieldFlags( int ff ) 437 { 438 field.setItem( "ClrFf", new COSInteger( ff ) ); 439 } 440 441 447 public Integer getWidgetFieldFlags() 448 { 449 Integer retval = null; 450 COSNumber f = (COSNumber)field.getDictionaryObject( "F" ); 451 if( f != null ) 452 { 453 retval = new Integer ( f.intValue() ); 454 } 455 return retval; 456 } 457 458 464 public void setWidgetFieldFlags( Integer f ) 465 { 466 COSInteger value = null; 467 if( f != null ) 468 { 469 value = new COSInteger( f.intValue() ); 470 } 471 field.setItem( "F", value ); 472 } 473 474 480 public void setWidgetFieldFlags( int f ) 481 { 482 field.setItem( "F", new COSInteger( f ) ); 483 } 484 485 491 public Integer getSetWidgetFieldFlags() 492 { 493 Integer retval = null; 494 COSNumber ff = (COSNumber)field.getDictionaryObject( "SetF" ); 495 if( ff != null ) 496 { 497 retval = new Integer ( ff.intValue() ); 498 } 499 return retval; 500 } 501 502 508 public void setSetWidgetFieldFlags( Integer ff ) 509 { 510 COSInteger value = null; 511 if( ff != null ) 512 { 513 value = new COSInteger( ff.intValue() ); 514 } 515 field.setItem( "SetF", value ); 516 } 517 518 524 public void setSetWidgetFieldFlags( int ff ) 525 { 526 field.setItem( "SetF", new COSInteger( ff ) ); 527 } 528 529 535 public Integer getClearWidgetFieldFlags() 536 { 537 Integer retval = null; 538 COSNumber ff = (COSNumber)field.getDictionaryObject( "ClrF" ); 539 if( ff != null ) 540 { 541 retval = new Integer ( ff.intValue() ); 542 } 543 return retval; 544 } 545 546 552 public void setClearWidgetFieldFlags( Integer ff ) 553 { 554 COSInteger value = null; 555 if( ff != null ) 556 { 557 value = new COSInteger( ff.intValue() ); 558 } 559 field.setItem( "ClrF", value ); 560 } 561 562 568 public void setClearWidgetFieldFlags( int ff ) 569 { 570 field.setItem( "ClrF", new COSInteger( ff ) ); 571 } 572 573 579 public PDAppearanceDictionary getAppearanceDictionary() 580 { 581 PDAppearanceDictionary retval = null; 582 COSDictionary dict = (COSDictionary)field.getDictionaryObject( "AP" ); 583 if( dict != null ) 584 { 585 retval = new PDAppearanceDictionary( dict ); 586 } 587 return retval; 588 } 589 590 595 public void setAppearanceDictionary( PDAppearanceDictionary ap ) 596 { 597 field.setItem( "AP", ap ); 598 } 599 600 605 public FDFNamedPageReference getAppearanceStreamReference() 606 { 607 FDFNamedPageReference retval = null; 608 COSDictionary ref = (COSDictionary)field.getDictionaryObject( "APRef" ); 609 if( ref != null ) 610 { 611 retval = new FDFNamedPageReference( ref ); 612 } 613 return retval; 614 } 615 616 621 public void setAppearanceStreamReference( FDFNamedPageReference ref ) 622 { 623 field.setItem( "APRef", ref ); 624 } 625 626 631 public FDFIconFit getIconFit() 632 { 633 FDFIconFit retval = null; 634 COSDictionary dic = (COSDictionary)field.getDictionaryObject( "IF" ); 635 if( dic != null ) 636 { 637 retval = new FDFIconFit( dic ); 638 } 639 return retval; 640 } 641 642 647 public void setIconFit( FDFIconFit fit ) 648 { 649 field.setItem( "IF", fit ); 650 } 651 652 658 public List getOptions() 659 { 660 List retval = null; 661 COSArray array = (COSArray)field.getDictionaryObject( "Opt" ); 662 if( array != null ) 663 { 664 List objects = new ArrayList (); 665 for( int i=0; i<array.size(); i++ ) 666 { 667 COSBase next = array.getObject( i ); 668 if( next instanceof COSString ) 669 { 670 objects.add( ((COSString)next).getString() ); 671 } 672 else 673 { 674 COSArray value = (COSArray)next; 675 objects.add( new FDFOptionElement( value ) ); 676 } 677 } 678 retval = new COSArrayList( objects, array ); 679 } 680 return retval; 681 } 682 683 689 public void setOptions( List options ) 690 { 691 COSArray value = COSArrayList.converterToCOSArray( options ); 692 field.setItem( "Opt", value ); 693 } 694 695 700 public PDAction getAction() 701 { 702 return PDActionFactory.createAction( (COSDictionary)field.getDictionaryObject( "A" ) ); 703 } 704 705 710 public void setAction( PDAction a ) 711 { 712 field.setItem( "A", a ); 713 } 714 715 721 public PDAdditionalActions getAdditionalActions() 722 { 723 PDAdditionalActions retval = null; 724 COSDictionary dict = (COSDictionary)field.getDictionaryObject( "AA" ); 725 if( dict != null ) 726 { 727 retval = new PDAdditionalActions( dict ); 728 } 729 730 return retval; 731 } 732 733 738 public void setAdditionalActions( PDAdditionalActions aa ) 739 { 740 field.setItem( "AA", aa ); 741 } 742 743 748 public PDTextStream getRichText() 749 { 750 COSBase rv = field.getDictionaryObject( "RV" ); 751 return PDTextStream.createTextStream( rv ); 752 } 753 754 759 public void setRichText( PDTextStream rv ) 760 { 761 field.setItem( "RV", rv ); 762 } 763 } | Popular Tags |