1 31 package org.pdfbox.pdmodel.interactive.annotation; 32 33 import org.pdfbox.cos.COSBase; 34 import org.pdfbox.cos.COSDictionary; 35 import org.pdfbox.cos.COSName; 36 import org.pdfbox.cos.COSStream; 37 38 import org.pdfbox.pdmodel.common.COSObjectable; 39 import org.pdfbox.pdmodel.common.COSDictionaryMap; 40 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 45 51 public class PDAppearanceDictionary implements COSObjectable 52 { 53 private COSDictionary dictionary; 54 55 58 public PDAppearanceDictionary() 59 { 60 dictionary = new COSDictionary(); 61 dictionary.setItem( COSName.getPDFName( "N" ), new COSDictionary() ); 63 } 64 65 70 public PDAppearanceDictionary( COSDictionary dict ) 71 { 72 dictionary = dict; 73 } 74 75 79 public COSDictionary getDictionary() 80 { 81 return dictionary; 82 } 83 84 88 public COSBase getCOSObject() 89 { 90 return dictionary; 91 } 92 93 100 public Map getNormalAppearance() 101 { 102 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "N" ) ); 103 if( ap instanceof COSStream ) 104 { 105 COSStream aux = (COSStream) ap; 106 ap = new COSDictionary(); 107 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux ); 108 } 109 COSDictionary map = (COSDictionary)ap; 110 Map actuals = new HashMap (); 111 Map retval = new COSDictionaryMap( actuals, map ); 112 Iterator asNames = map.keyList().iterator(); 113 while( asNames.hasNext() ) 114 { 115 COSName asName = (COSName)asNames.next(); 116 COSStream as = (COSStream)map.getDictionaryObject( asName ); 117 actuals.put( asName.getName(), new PDAppearanceStream( as ) ); 118 } 119 120 return retval; 121 } 122 123 130 public void setNormalAppearance( Map appearanceMap ) 131 { 132 dictionary.setItem( COSName.getPDFName( "N" ), COSDictionaryMap.convert( appearanceMap ) ); 133 } 134 135 141 public void setNormalAppearance( PDAppearanceStream ap ) 142 { 143 dictionary.setItem( COSName.getPDFName( "N" ), ap.getStream() ); 144 } 145 146 154 public Map getRolloverAppearance() 155 { 156 Map retval = null; 157 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "R" ) ); 158 if( ap == null ) 159 { 160 retval = getNormalAppearance(); 161 } 162 else 163 { 164 if( ap instanceof COSStream ) 165 { 166 ap = new COSDictionary(); 167 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), ap ); 168 } 169 COSDictionary map = (COSDictionary)ap; 170 Map actuals = new HashMap (); 171 retval = new COSDictionaryMap( actuals, map ); 172 Iterator asNames = map.keyList().iterator(); 173 while( asNames.hasNext() ) 174 { 175 COSName asName = (COSName)asNames.next(); 176 COSStream as = (COSStream)map.getDictionaryObject( asName ); 177 actuals.put( asName.getName(), new PDAppearanceStream( as ) ); 178 } 179 } 180 181 return retval; 182 } 183 184 191 public void setRolloverAppearance( Map appearanceMap ) 192 { 193 dictionary.setItem( COSName.getPDFName( "R" ), COSDictionaryMap.convert( appearanceMap ) ); 194 } 195 196 204 public Map getDownAppearance() 205 { 206 Map retval = null; 207 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "D" ) ); 208 if( ap == null ) 209 { 210 retval = getNormalAppearance(); 211 } 212 else 213 { 214 if( ap instanceof COSStream ) 215 { 216 ap = new COSDictionary(); 217 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), ap ); 218 } 219 COSDictionary map = (COSDictionary)ap; 220 Map actuals = new HashMap (); 221 retval = new COSDictionaryMap( actuals, map ); 222 Iterator asNames = map.keyList().iterator(); 223 while( asNames.hasNext() ) 224 { 225 COSName asName = (COSName)asNames.next(); 226 COSStream as = (COSStream)map.getDictionaryObject( asName ); 227 actuals.put( asName.getName(), new PDAppearanceStream( as ) ); 228 } 229 } 230 231 return retval; 232 } 233 234 241 public void setDownAppearance( Map appearanceMap ) 242 { 243 dictionary.setItem( COSName.getPDFName( "D" ), COSDictionaryMap.convert( appearanceMap ) ); 244 } 245 } | Popular Tags |