KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > PDResources


1 /**
2  * Copyright (c) 2003-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel;
32
33 import java.io.IOException JavaDoc;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
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 /**
57  * This represents a set of resources available at the page/pages/stream level.
58  *
59  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
60  * @version $Revision: 1.16 $
61  */

62 public class PDResources implements COSObjectable
63 {
64     private COSDictionary resources;
65
66     /**
67      * Default constructor.
68      */

69     public PDResources()
70     {
71         resources = new COSDictionary();
72     }
73
74     /**
75      * Prepopulated resources.
76      *
77      * @param resourceDictionary The cos dictionary for this resource.
78      */

79     public PDResources( COSDictionary resourceDictionary )
80     {
81         resources = resourceDictionary;
82     }
83
84     /**
85      * This will get the underlying dictionary.
86      *
87      * @return The dictionary for these resources.
88      */

89     public COSDictionary getCOSDictionary()
90     {
91         return resources;
92     }
93     
94     /**
95      * Convert this standard java object to a COS object.
96      *
97      * @return The cos object that matches this Java object.
98      */

99     public COSBase getCOSObject()
100     {
101         return resources;
102     }
103     
104     /**
105      * This will get the map of fonts. This will never return null. The keys are string
106      * and the values are PDFont objects.
107      *
108      * @param fontCache A map of existing PDFont objects to reuse.
109      * @return The map of fonts.
110      *
111      * @throws IOException If there is an error getting the fonts.
112      */

113     public Map JavaDoc getFonts( Map JavaDoc fontCache ) throws IOException JavaDoc
114     {
115         Map JavaDoc 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 JavaDoc actuals = new HashMap JavaDoc();
125         retval = new COSDictionaryMap( actuals, fonts );
126         Iterator JavaDoc fontNames = fonts.keyList().iterator();
127         while( fontNames.hasNext() )
128         {
129             COSName fontName = (COSName)fontNames.next();
130             COSBase font = fonts.getDictionaryObject( fontName );
131             //data-000174.pdf contains a font that is a COSArray, looks to be an error in the
132
//PDF, we will just ignore entries that are not dictionaries.
133
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     /**
143      * This will get the map of fonts. This will never return null. The keys are string
144      * and the values are PDFont objects.
145      *
146      * @return The map of fonts.
147      *
148      * @throws IOException If there is an error getting the fonts.
149      */

150     public Map JavaDoc getFonts() throws IOException JavaDoc
151     {
152         return getFonts( null );
153     }
154     
155     /**
156      * This will get the map of PDXObjects that are in the resource dictionary.
157      *
158      * @return The map of xobjects.
159      *
160      * @throws IOException If there is an error creating the xobjects.
161      */

162     public Map JavaDoc getXObjects() throws IOException JavaDoc
163     {
164         Map JavaDoc 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 JavaDoc actuals = new HashMap JavaDoc();
174         retval = new COSDictionaryMap( actuals, xobjects );
175         Iterator JavaDoc 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     /**
190      * This will get the map of images. An empty map will be returned if there
191      * are no underlying images.
192      * So far the keys are COSName of the image
193      * and the value is the corresponding PDXObjectImage.
194      *
195      * @author By BM
196      * @return The map of images.
197      * @throws IOException If there is an error writing the picture.
198      */

199     public Map JavaDoc getImages() throws IOException JavaDoc
200     {
201         Map JavaDoc 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 JavaDoc actuals = new HashMap JavaDoc();
211         retval = new COSDictionaryMap( actuals, images );
212         Iterator JavaDoc 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     /**
232      * This will set the map of fonts.
233      *
234      * @param fonts The new map of fonts.
235      */

236     public void setFonts( Map JavaDoc fonts )
237     {
238         resources.setItem( COSName.FONT, COSDictionaryMap.convert( fonts ) );
239     }
240
241     /**
242      * This will get the map of colorspaces. This will return null if the underlying
243      * resources dictionary does not have a colorspace dictionary. The keys are string
244      * and the values are PDColorSpace objects.
245      *
246      * @return The map of colorspaces.
247      *
248      * @throws IOException If there is an error getting the colorspaces.
249      */

250     public Map JavaDoc getColorSpaces() throws IOException JavaDoc
251     {
252         Map JavaDoc retval = null;
253         COSDictionary colorspaces = (COSDictionary)resources.getDictionaryObject( COSName.getPDFName( "ColorSpace" ) );
254
255         if( colorspaces != null )
256         {
257             Map JavaDoc actuals = new HashMap JavaDoc();
258             retval = new COSDictionaryMap( actuals, colorspaces );
259             Iterator JavaDoc 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     /**
271      * This will set the map of colorspaces.
272      *
273      * @param colorspaces The new map of colorspaces.
274      */

275     public void setColorSpaces( Map JavaDoc colorspaces )
276     {
277         resources.setItem( COSName.getPDFName( "ColorSpace" ), COSDictionaryMap.convert( colorspaces ) );
278     }
279
280     /**
281      * This will get the map of graphic states. This will return null if the underlying
282      * resources dictionary does not have a graphics dictionary. The keys are the graphic state
283      * name as a String and the values are PDExtendedGraphicsState objects.
284      *
285      * @return The map of extended graphic state objects.
286      */

287     public Map JavaDoc getGraphicsStates()
288     {
289         Map JavaDoc retval = null;
290         COSDictionary states = (COSDictionary)resources.getDictionaryObject( COSName.getPDFName( "ExtGState" ) );
291
292         if( states != null )
293         {
294             Map JavaDoc actuals = new HashMap JavaDoc();
295             retval = new COSDictionaryMap( actuals, states );
296             Iterator JavaDoc 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     /**
308      * This will set the map of graphics states.
309      *
310      * @param states The new map of states.
311      */

312     public void setGraphicsStates( Map JavaDoc states )
313     {
314         Iterator JavaDoc iter = states.keySet().iterator();
315         COSDictionary dic = new COSDictionary();
316         while( iter.hasNext() )
317         {
318             String JavaDoc name = (String JavaDoc)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