1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.interactive.form; 18 19 import java.io.IOException; 20 21 import org.apache.pdfbox.cos.COSArray; 22 import org.apache.pdfbox.cos.COSBase; 23 import org.apache.pdfbox.cos.COSDictionary; 24 import org.apache.pdfbox.cos.COSName; 25 import org.apache.pdfbox.cos.COSString; 26 import org.apache.pdfbox.cos.COSInteger; 27 28 /** 29 * A class for handling the PDF field as a choicefield. 30 * 31 * @author sug 32 * @version $Revision: 1.7 $ 33 */ 34 public class PDChoiceField extends PDVariableText 35 { 36 37 /** 38 * @see org.apache.pdfbox.pdmodel.interactive.form.PDField#PDField(PDAcroForm,COSDictionary) 39 * 40 * @param theAcroForm The acroForm for this field. 41 * @param field The field for this choice field. 42 */ 43 public PDChoiceField( PDAcroForm theAcroForm, COSDictionary field) 44 { 45 super(theAcroForm, field); 46 } 47 48 /** 49 * @see org.apache.pdfbox.pdmodel.interactive.form.PDField#setValue(java.lang.String) 50 * 51 * @param optionValue The new value for this text field. 52 * 53 * @throws IOException If there is an error calculating the appearance stream or the value in not one 54 * of the existing options. 55 */ 56 public void setValue(String optionValue) throws IOException 57 { 58 int indexSelected = -1; 59 COSArray options = (COSArray)getDictionary().getDictionaryObject( "Opt" ); 60 if( options.size() == 0 ) 61 { 62 throw new IOException( "Error: You cannot set a value for a choice field if there are no options." ); 63 } 64 else 65 { 66 COSBase option = options.getObject( 0 ); 67 if( option instanceof COSArray ) 68 { 69 for( int i=0; i<options.size() && indexSelected == -1; i++ ) 70 { 71 COSArray keyValuePair = (COSArray)options.get( i ); 72 COSString key = (COSString)keyValuePair.getObject( 0 ); 73 COSString value = (COSString)keyValuePair.getObject( 1 ); 74 if( optionValue.equals( key.getString() ) || optionValue.equals( value.getString() ) ) 75 { 76 //have the parent draw the appearance stream with the value 77 super.setValue( value.getString() ); 78 //but then use the key as the V entry 79 getDictionary().setItem( COSName.getPDFName( "V" ), key ); 80 indexSelected = i; 81 } 82 } 83 } 84 else 85 { 86 for( int i=0; i<options.size() && indexSelected == -1; i++ ) 87 { 88 COSString value = (COSString)options.get( i ); 89 if( optionValue.equals( value.getString() ) ) 90 { 91 super.setValue( optionValue ); 92 indexSelected = i; 93 } 94 } 95 } 96 } 97 if( indexSelected == -1 ) 98 { 99 throw new IOException( "Error: '" + optionValue + "' was not an available option."); 100 } 101 else 102 { 103 COSArray indexArray = (COSArray)getDictionary().getDictionaryObject( "I" ); 104 if( indexArray != null ) 105 { 106 indexArray.clear(); 107 indexArray.add( COSInteger.get( indexSelected ) ); 108 } 109 } 110 } 111 112 113 }