1 31 package org.pdfbox.pdmodel.interactive.form; 32 33 import org.pdfbox.cos.COSBase; 34 import org.pdfbox.cos.COSDictionary; 35 import org.pdfbox.cos.COSName; 36 37 import java.io.IOException ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 48 public class PDCheckbox extends PDChoiceButton 49 { 50 private static final COSName KEY = COSName.getPDFName("AS"); 51 private static final COSName OFF_VALUE = COSName.getPDFName("Off"); 52 53 private COSName value; 54 55 61 public PDCheckbox( PDAcroForm theAcroForm, COSDictionary field) 62 { 63 super( theAcroForm, field); 64 COSDictionary ap = (COSDictionary) field.getDictionaryObject(COSName.getPDFName("AP")); 65 if( ap != null ) 66 { 67 COSBase n = ap.getDictionaryObject(COSName.getPDFName("N")); 68 69 if( n instanceof COSDictionary ) 70 { 71 List li = ((COSDictionary)n).keyList(); 72 for( int i=0; i<li.size(); i++ ) 73 { 74 COSName name = (COSName)li.get( i ); 75 if( !name.equals( OFF_VALUE )) 76 { 77 value = name; 78 } 79 } 80 81 } 82 } 83 else 84 { 85 value = (COSName)getDictionary().getDictionaryObject( "V" ); 86 } 87 } 88 89 94 public boolean isChecked() 95 { 96 boolean retval = false; 97 String onValue = getOnValue(); 98 COSName radioValue = (COSName)getDictionary().getDictionaryObject( KEY ); 99 if( radioValue != null && value != null && radioValue.getName().equals( onValue ) ) 100 { 101 retval = true; 102 } 103 104 return retval; 105 } 106 107 110 public void check() 111 { 112 getDictionary().setItem(KEY, value); 113 } 114 115 118 public void unCheck() 119 { 120 getDictionary().setItem(KEY, OFF_VALUE); 121 } 122 123 126 public void setValue(String newValue) 127 { 128 getDictionary().setName( "V", newValue ); 129 if( newValue == null ) 130 { 131 getDictionary().setItem( KEY, OFF_VALUE ); 132 } 133 else 134 { 135 getDictionary().setName( KEY, newValue ); 136 } 137 } 138 139 144 public String getOffValue() 145 { 146 return OFF_VALUE.getName(); 147 } 148 149 154 public String getOnValue() 155 { 156 String retval = null; 157 COSDictionary ap = (COSDictionary) getDictionary().getDictionaryObject(COSName.getPDFName("AP")); 158 COSBase n = ap.getDictionaryObject(COSName.getPDFName("N")); 159 160 if( n instanceof COSDictionary ) 162 { 163 Iterator li = ((COSDictionary)n).keyList().iterator(); 164 while( li.hasNext() ) 165 { 166 Object key = li.next(); 167 if( !key.equals( OFF_VALUE) ) 168 { 169 retval = ((COSName)key).getName(); 170 } 171 } 172 } 173 return retval; 174 } 175 176 183 public String getValue() throws IOException 184 { 185 return getDictionary().getNameAsString( "V" ); 186 } 187 188 } | Popular Tags |