KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > interactive > annotation > PDAppearanceDictionary


1 /**
2  * Copyright (c) 2003-2004, 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.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 JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Map JavaDoc;
44
45 /**
46  * This class represents a PDF /AP entry the appearance dictionary.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.4 $
50  */

51 public class PDAppearanceDictionary implements COSObjectable
52 {
53     private COSDictionary dictionary;
54
55     /**
56      * Constructor.
57      */

58     public PDAppearanceDictionary()
59     {
60         dictionary = new COSDictionary();
61         //the N entry is required.
62
dictionary.setItem( COSName.getPDFName( "N" ), new COSDictionary() );
63     }
64
65     /**
66      * Constructor.
67      *
68      * @param dict The annotations dictionary.
69      */

70     public PDAppearanceDictionary( COSDictionary dict )
71     {
72         dictionary = dict;
73     }
74
75     /**
76      * returns the dictionary.
77      * @return the dictionary
78      */

79     public COSDictionary getDictionary()
80     {
81         return dictionary;
82     }
83
84     /**
85      * returns the dictionary.
86      * @return the dictionary
87      */

88     public COSBase getCOSObject()
89     {
90         return dictionary;
91     }
92
93     /**
94      * This will return a list of appearances. In the case where there is
95      * only one appearance the map will contain one entry whose key is the string
96      * "default".
97      *
98      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
99      */

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 JavaDoc();
111         Map retval = new COSDictionaryMap( actuals, map );
112         Iterator JavaDoc 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     /**
124      * This will set a list of appearances. If you would like to set the single
125      * appearance then you should use the key "default", and when the PDF is written
126      * back to the filesystem then there will only be one stream.
127      *
128      * @param appearanceMap The updated map with the appearance.
129      */

130     public void setNormalAppearance( Map appearanceMap )
131     {
132         dictionary.setItem( COSName.getPDFName( "N" ), COSDictionaryMap.convert( appearanceMap ) );
133     }
134
135     /**
136      * This will set the normal appearance when there is only one appearance
137      * to be shown.
138      *
139      * @param ap The appearance stream to show.
140      */

141     public void setNormalAppearance( PDAppearanceStream ap )
142     {
143         dictionary.setItem( COSName.getPDFName( "N" ), ap.getStream() );
144     }
145
146     /**
147      * This will return a list of appearances. In the case where there is
148      * only one appearance the map will contain one entry whose key is the string
149      * "default". If there is no rollover appearance then the normal appearance
150      * will be returned. Which means that this method will never return null.
151      *
152      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
153      */

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 JavaDoc();
171             retval = new COSDictionaryMap( actuals, map );
172             Iterator JavaDoc 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     /**
185      * This will set a list of appearances. If you would like to set the single
186      * appearance then you should use the key "default", and when the PDF is written
187      * back to the filesystem then there will only be one stream.
188      *
189      * @param appearanceMap The updated map with the appearance.
190      */

191     public void setRolloverAppearance( Map appearanceMap )
192     {
193         dictionary.setItem( COSName.getPDFName( "R" ), COSDictionaryMap.convert( appearanceMap ) );
194     }
195
196     /**
197      * This will return a list of appearances. In the case where there is
198      * only one appearance the map will contain one entry whose key is the string
199      * "default". If there is no rollover appearance then the normal appearance
200      * will be returned. Which means that this method will never return null.
201      *
202      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
203      */

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 JavaDoc();
221             retval = new COSDictionaryMap( actuals, map );
222             Iterator JavaDoc 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     /**
235      * This will set a list of appearances. If you would like to set the single
236      * appearance then you should use the key "default", and when the PDF is written
237      * back to the filesystem then there will only be one stream.
238      *
239      * @param appearanceMap The updated map with the appearance.
240      */

241     public void setDownAppearance( Map appearanceMap )
242     {
243         dictionary.setItem( COSName.getPDFName( "D" ), COSDictionaryMap.convert( appearanceMap ) );
244     }
245 }
Popular Tags