1 31 package org.pdfbox.pdmodel.interactive.form; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.cos.COSArray; 36 import org.pdfbox.cos.COSBase; 37 import org.pdfbox.cos.COSDictionary; 38 import org.pdfbox.cos.COSName; 39 import org.pdfbox.cos.COSString; 40 import org.pdfbox.cos.COSInteger; 41 42 48 public class PDChoiceField extends PDVariableText 49 { 50 51 57 public PDChoiceField( PDAcroForm theAcroForm, COSDictionary field) 58 { 59 super(theAcroForm, field); 60 } 61 62 70 public void setValue(String optionValue) throws IOException 71 { 72 int indexSelected = -1; 73 COSArray options = (COSArray)getDictionary().getDictionaryObject( "Opt" ); 74 if( options.size() == 0 ) 75 { 76 throw new IOException ( "Error: You cannot set a value for a choice field if there are no options." ); 77 } 78 else 79 { 80 COSBase option = options.getObject( 0 ); 81 if( option instanceof COSArray ) 82 { 83 for( int i=0; i<options.size() && indexSelected == -1; i++ ) 84 { 85 COSArray keyValuePair = (COSArray)options.get( i ); 86 COSString key = (COSString)keyValuePair.getObject( 0 ); 87 COSString value = (COSString)keyValuePair.getObject( 1 ); 88 if( optionValue.equals( key.getString() ) || optionValue.equals( value.getString() ) ) 89 { 90 super.setValue( value.getString() ); 92 getDictionary().setItem( COSName.getPDFName( "V" ), key ); 94 indexSelected = i; 95 } 96 } 97 } 98 else 99 { 100 for( int i=0; i<options.size() && indexSelected == -1; i++ ) 101 { 102 COSString value = (COSString)options.get( i ); 103 if( optionValue.equals( value.getString() ) ) 104 { 105 super.setValue( optionValue ); 106 indexSelected = i; 107 } 108 } 109 } 110 } 111 if( indexSelected == -1 ) 112 { 113 throw new IOException ( "Error: '" + optionValue + "' was not an available option."); 114 } 115 else 116 { 117 COSArray indexArray = (COSArray)getDictionary().getDictionaryObject( "I" ); 118 if( indexArray != null ) 119 { 120 indexArray.clear(); 121 indexArray.add( new COSInteger( indexSelected ) ); 122 } 123 } 124 } 125 126 127 } | Popular Tags |