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 org.apache.pdfbox.cos.COSBase; 20 import org.apache.pdfbox.cos.COSDictionary; 21 import org.apache.pdfbox.cos.COSName; 22 23 import java.io.IOException; 24 25 /** 26 * A class for handling the PDF field as a checkbox. 27 * 28 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 29 * @author sug 30 * @version $Revision: 1.11 $ 31 */ 32 public class PDCheckbox extends PDChoiceButton 33 { 34 private static final COSName KEY = COSName.getPDFName("AS"); 35 private static final COSName OFF_VALUE = COSName.getPDFName("Off"); 36 37 private COSName value; 38 39 /** 40 * @see PDField#PDField(PDAcroForm,COSDictionary) 41 * 42 * @param theAcroForm The acroForm for this field. 43 * @param field The checkbox field dictionary 44 */ 45 public PDCheckbox( PDAcroForm theAcroForm, COSDictionary field) 46 { 47 super( theAcroForm, field); 48 COSDictionary ap = (COSDictionary) field.getDictionaryObject(COSName.getPDFName("AP")); 49 if( ap != null ) 50 { 51 COSBase n = ap.getDictionaryObject(COSName.getPDFName("N")); 52 53 if( n instanceof COSDictionary ) 54 { 55 for( COSName name : ((COSDictionary)n).keySet() ) 56 { 57 if( !name.equals( OFF_VALUE )) 58 { 59 value = name; 60 } 61 } 62 63 } 64 } 65 else 66 { 67 value = (COSName)getDictionary().getDictionaryObject( "V" ); 68 } 69 } 70 71 /** 72 * This will tell if this radio button is currently checked or not. 73 * 74 * @return true If the radio button is checked. 75 */ 76 public boolean isChecked() 77 { 78 boolean retval = false; 79 String onValue = getOnValue(); 80 COSName radioValue = (COSName)getDictionary().getDictionaryObject( KEY ); 81 if( radioValue != null && value != null && radioValue.getName().equals( onValue ) ) 82 { 83 retval = true; 84 } 85 86 return retval; 87 } 88 89 /** 90 * Checks the radiobutton. 91 */ 92 public void check() 93 { 94 getDictionary().setItem(KEY, value); 95 } 96 97 /** 98 * Unchecks the radiobutton. 99 */ 100 public void unCheck() 101 { 102 getDictionary().setItem(KEY, OFF_VALUE); 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 public void setValue(String newValue) 109 { 110 getDictionary().setName( "V", newValue ); 111 if( newValue == null ) 112 { 113 getDictionary().setItem( KEY, OFF_VALUE ); 114 } 115 else 116 { 117 getDictionary().setName( KEY, newValue ); 118 } 119 } 120 121 /** 122 * This will get the value of the radio button. 123 * 124 * @return The value of the radio button. 125 */ 126 public String getOffValue() 127 { 128 return OFF_VALUE.getName(); 129 } 130 131 /** 132 * This will get the value of the radio button. 133 * 134 * @return The value of the radio button. 135 */ 136 public String getOnValue() 137 { 138 String retval = null; 139 COSDictionary ap = (COSDictionary) getDictionary().getDictionaryObject(COSName.getPDFName("AP")); 140 COSBase n = ap.getDictionaryObject(COSName.getPDFName("N")); 141 142 //N can be a COSDictionary or a COSStream 143 if( n instanceof COSDictionary ) 144 { 145 for( COSName key :((COSDictionary)n).keySet() ) 146 { 147 if( !key.equals( OFF_VALUE) ) 148 { 149 retval = key.getName(); 150 } 151 } 152 } 153 return retval; 154 } 155 156 /** 157 * getValue gets the fields value to as a string. 158 * 159 * @return The string value of this field. 160 * 161 * @throws IOException If there is an error getting the value. 162 */ 163 public String getValue() throws IOException 164 { 165 return getDictionary().getNameAsString( "V" ); 166 } 167 168 }