1 31 package org.pdfbox.pdmodel; 32 33 import java.io.IOException ; 34 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 39 import org.pdfbox.cos.COSBase; 40 import org.pdfbox.cos.COSDictionary; 41 import org.pdfbox.cos.COSName; 42 import org.pdfbox.cos.COSStream; 43 44 import org.pdfbox.pdmodel.common.COSDictionaryMap; 45 import org.pdfbox.pdmodel.common.COSObjectable; 46 47 import org.pdfbox.pdmodel.font.PDFontFactory; 48 49 import org.pdfbox.pdmodel.graphics.PDExtendedGraphicsState; 50 51 import org.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory; 52 53 import org.pdfbox.pdmodel.graphics.xobject.PDXObject; 54 import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage; 55 56 62 public class PDResources implements COSObjectable 63 { 64 private COSDictionary resources; 65 66 69 public PDResources() 70 { 71 resources = new COSDictionary(); 72 } 73 74 79 public PDResources( COSDictionary resourceDictionary ) 80 { 81 resources = resourceDictionary; 82 } 83 84 89 public COSDictionary getCOSDictionary() 90 { 91 return resources; 92 } 93 94 99 public COSBase getCOSObject() 100 { 101 return resources; 102 } 103 104 113 public Map getFonts( Map fontCache ) throws IOException 114 { 115 Map retval = null; 116 COSDictionary fonts = (COSDictionary)resources.getDictionaryObject( COSName.FONT ); 117 118 if( fonts == null ) 119 { 120 fonts = new COSDictionary(); 121 resources.setItem( COSName.FONT, fonts ); 122 } 123 124 Map actuals = new HashMap (); 125 retval = new COSDictionaryMap( actuals, fonts ); 126 Iterator fontNames = fonts.keyList().iterator(); 127 while( fontNames.hasNext() ) 128 { 129 COSName fontName = (COSName)fontNames.next(); 130 COSBase font = fonts.getDictionaryObject( fontName ); 131 if( font instanceof COSDictionary ) 134 { 135 COSDictionary fontDictionary = (COSDictionary)font; 136 actuals.put( fontName.getName(), PDFontFactory.createFont( fontDictionary, fontCache ) ); 137 } 138 } 139 return retval; 140 } 141 142 150 public Map getFonts() throws IOException 151 { 152 return getFonts( null ); 153 } 154 155 162 public Map getXObjects() throws IOException 163 { 164 Map retval = null; 165 COSDictionary xobjects = (COSDictionary)resources.getDictionaryObject( "XObject" ); 166 167 if( xobjects == null ) 168 { 169 xobjects = new COSDictionary(); 170 resources.setItem( "XObject", xobjects ); 171 } 172 173 Map actuals = new HashMap (); 174 retval = new COSDictionaryMap( actuals, xobjects ); 175 Iterator imageNames = xobjects.keyList().iterator(); 176 while( imageNames.hasNext() ) 177 { 178 COSName objName = (COSName)imageNames.next(); 179 COSBase cosObject = xobjects.getDictionaryObject(objName); 180 PDXObject xobject = PDXObject.createXObject( cosObject ); 181 if( xobject !=null ) 182 { 183 actuals.put( objName.getName(), xobject); 184 } 185 } 186 return retval; 187 } 188 189 199 public Map getImages() throws IOException 200 { 201 Map retval = null; 202 COSDictionary images = (COSDictionary)resources.getDictionaryObject( "XObject" ); 203 204 if( images == null ) 205 { 206 images = new COSDictionary(); 207 resources.setItem( "XObject", images ); 208 } 209 210 Map actuals = new HashMap (); 211 retval = new COSDictionaryMap( actuals, images ); 212 Iterator imageNames = images.keyList().iterator(); 213 while( imageNames.hasNext() ) 214 { 215 COSName imageName = (COSName)imageNames.next(); 216 COSStream image = (COSStream)(images.getDictionaryObject(imageName)); 217 218 COSName subType =(COSName)image.getDictionaryObject(COSName.SUBTYPE); 219 if( subType.equals(COSName.IMAGE) ) 220 { 221 PDXObjectImage ximage = (PDXObjectImage)PDXObject.createXObject( image ); 222 if( ximage !=null ) 223 { 224 actuals.put( imageName.getName(), ximage); 225 } 226 } 227 } 228 return retval; 229 } 230 231 236 public void setFonts( Map fonts ) 237 { 238 resources.setItem( COSName.FONT, COSDictionaryMap.convert( fonts ) ); 239 } 240 241 250 public Map getColorSpaces() throws IOException 251 { 252 Map retval = null; 253 COSDictionary colorspaces = (COSDictionary)resources.getDictionaryObject( COSName.getPDFName( "ColorSpace" ) ); 254 255 if( colorspaces != null ) 256 { 257 Map actuals = new HashMap (); 258 retval = new COSDictionaryMap( actuals, colorspaces ); 259 Iterator csNames = colorspaces.keyList().iterator(); 260 while( csNames.hasNext() ) 261 { 262 COSName csName = (COSName)csNames.next(); 263 COSBase cs = colorspaces.getDictionaryObject( csName ); 264 actuals.put( csName.getName(), PDColorSpaceFactory.createColorSpace( cs ) ); 265 } 266 } 267 return retval; 268 } 269 270 275 public void setColorSpaces( Map colorspaces ) 276 { 277 resources.setItem( COSName.getPDFName( "ColorSpace" ), COSDictionaryMap.convert( colorspaces ) ); 278 } 279 280 287 public Map getGraphicsStates() 288 { 289 Map retval = null; 290 COSDictionary states = (COSDictionary)resources.getDictionaryObject( COSName.getPDFName( "ExtGState" ) ); 291 292 if( states != null ) 293 { 294 Map actuals = new HashMap (); 295 retval = new COSDictionaryMap( actuals, states ); 296 Iterator names = states.keyList().iterator(); 297 while( names.hasNext() ) 298 { 299 COSName name = (COSName)names.next(); 300 COSDictionary dictionary = (COSDictionary)states.getDictionaryObject( name ); 301 actuals.put( name.getName(), new PDExtendedGraphicsState( dictionary ) ); 302 } 303 } 304 return retval; 305 } 306 307 312 public void setGraphicsStates( Map states ) 313 { 314 Iterator iter = states.keySet().iterator(); 315 COSDictionary dic = new COSDictionary(); 316 while( iter.hasNext() ) 317 { 318 String name = (String )iter.next(); 319 PDExtendedGraphicsState state = (PDExtendedGraphicsState)states.get( name ); 320 dic.setItem( COSName.getPDFName( name ), state.getCOSObject() ); 321 } 322 resources.setItem( COSName.getPDFName( "ExtGState" ), dic ); 323 } 324 } | Popular Tags |