]> _ Git - cubeextranet.git/blob
e3d029a086188bf3c717f7e001709a7d456e70ed
[cubeextranet.git] /
1 /*\r
2  * Licensed to the Apache Software Foundation (ASF) under one or more\r
3  * contributor license agreements.  See the NOTICE file distributed with\r
4  * this work for additional information regarding copyright ownership.\r
5  * The ASF licenses this file to You under the Apache License, Version 2.0\r
6  * (the "License"); you may not use this file except in compliance with\r
7  * the License.  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 package org.apache.pdfbox.pdmodel.interactive.annotation;\r
18 \r
19 import org.apache.pdfbox.cos.COSBase;\r
20 import org.apache.pdfbox.cos.COSDictionary;\r
21 import org.apache.pdfbox.cos.COSName;\r
22 import org.apache.pdfbox.cos.COSStream;\r
23 \r
24 import org.apache.pdfbox.pdmodel.common.COSObjectable;\r
25 import org.apache.pdfbox.pdmodel.common.COSDictionaryMap;\r
26 \r
27 import java.util.HashMap;\r
28 import java.util.Map;\r
29 \r
30 /**\r
31  * This class represents a PDF /AP entry the appearance dictionary.\r
32  *\r
33  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>\r
34  * @version $Revision: 1.4 $\r
35  */\r
36 public class PDAppearanceDictionary implements COSObjectable\r
37 {\r
38     private COSDictionary dictionary;\r
39 \r
40     /**\r
41      * Constructor.\r
42      */\r
43     public PDAppearanceDictionary()\r
44     {\r
45         dictionary = new COSDictionary();\r
46         //the N entry is required.\r
47         dictionary.setItem( COSName.getPDFName( "N" ), new COSDictionary() );\r
48     }\r
49 \r
50     /**\r
51      * Constructor.\r
52      *\r
53      * @param dict The annotations dictionary.\r
54      */\r
55     public PDAppearanceDictionary( COSDictionary dict )\r
56     {\r
57         dictionary = dict;\r
58     }\r
59 \r
60     /**\r
61      * returns the dictionary.\r
62      * @return the dictionary\r
63      */\r
64     public COSDictionary getDictionary()\r
65     {\r
66         return dictionary;\r
67     }\r
68 \r
69     /**\r
70      * returns the dictionary.\r
71      * @return the dictionary\r
72      */\r
73     public COSBase getCOSObject()\r
74     {\r
75         return dictionary;\r
76     }\r
77 \r
78     /**\r
79      * This will return a list of appearances.  In the case where there is\r
80      * only one appearance the map will contain one entry whose key is the string\r
81      * "default".\r
82      *\r
83      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs\r
84      */\r
85     public Map getNormalAppearance()\r
86     {\r
87         COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "N" ) );\r
88         if ( ap == null )\r
89         { \r
90             return null; \r
91         }\r
92         else if( ap instanceof COSStream )\r
93         {\r
94             COSStream aux = (COSStream) ap;\r
95             ap = new COSDictionary();\r
96             ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );\r
97         }\r
98         COSDictionary map = (COSDictionary)ap;\r
99         Map<String, PDAppearanceStream> actuals = new HashMap<String, PDAppearanceStream>();\r
100         Map retval = new COSDictionaryMap( actuals, map );\r
101         for( COSName asName : map.keySet() )\r
102         {\r
103             COSStream as = (COSStream)map.getDictionaryObject( asName );\r
104             actuals.put( asName.getName(), new PDAppearanceStream( as ) );\r
105         }\r
106 \r
107         return retval;\r
108     }\r
109 \r
110     /**\r
111      * This will set a list of appearances.  If you would like to set the single\r
112      * appearance then you should use the key "default", and when the PDF is written\r
113      * back to the filesystem then there will only be one stream.\r
114      *\r
115      * @param appearanceMap The updated map with the appearance.\r
116      */\r
117     public void setNormalAppearance( Map appearanceMap )\r
118     {\r
119         dictionary.setItem( COSName.getPDFName( "N" ), COSDictionaryMap.convert( appearanceMap ) );\r
120     }\r
121 \r
122     /**\r
123      * This will set the normal appearance when there is only one appearance\r
124      * to be shown.\r
125      *\r
126      * @param ap The appearance stream to show.\r
127      */\r
128     public void setNormalAppearance( PDAppearanceStream ap )\r
129     {\r
130         dictionary.setItem( COSName.getPDFName( "N" ), ap.getStream() );\r
131     }\r
132 \r
133     /**\r
134      * This will return a list of appearances.  In the case where there is\r
135      * only one appearance the map will contain one entry whose key is the string\r
136      * "default".  If there is no rollover appearance then the normal appearance\r
137      * will be returned.  Which means that this method will never return null.\r
138      *\r
139      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs\r
140      */\r
141     public Map getRolloverAppearance()\r
142     {\r
143         Map retval = null;\r
144         COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "R" ) );\r
145         if( ap == null )\r
146         {\r
147             retval = getNormalAppearance();\r
148         }\r
149         else\r
150         {\r
151             if( ap instanceof COSStream )\r
152             {\r
153                 COSStream aux = (COSStream) ap;\r
154                 ap = new COSDictionary();\r
155                 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );\r
156             }\r
157             COSDictionary map = (COSDictionary)ap;\r
158             Map<String, PDAppearanceStream> actuals = new HashMap<String, PDAppearanceStream>();\r
159             retval = new COSDictionaryMap( actuals, map );\r
160             for( COSName asName : map.keySet() )\r
161             {\r
162                 COSStream as = (COSStream)map.getDictionaryObject( asName );\r
163                 actuals.put( asName.getName(), new PDAppearanceStream( as ) );\r
164             }\r
165         }\r
166 \r
167         return retval;\r
168     }\r
169 \r
170     /**\r
171      * This will set a list of appearances.  If you would like to set the single\r
172      * appearance then you should use the key "default", and when the PDF is written\r
173      * back to the filesystem then there will only be one stream.\r
174      *\r
175      * @param appearanceMap The updated map with the appearance.\r
176      */\r
177     public void setRolloverAppearance( Map appearanceMap )\r
178     {\r
179         dictionary.setItem( COSName.getPDFName( "R" ), COSDictionaryMap.convert( appearanceMap ) );\r
180     }\r
181 \r
182     /**\r
183      * This will return a list of appearances.  In the case where there is\r
184      * only one appearance the map will contain one entry whose key is the string\r
185      * "default".  If there is no rollover appearance then the normal appearance\r
186      * will be returned.  Which means that this method will never return null.\r
187      *\r
188      * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs\r
189      */\r
190     public Map getDownAppearance()\r
191     {\r
192         Map retval = null;\r
193         COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "D" ) );\r
194         if( ap == null )\r
195         {\r
196             retval = getNormalAppearance();\r
197         }\r
198         else\r
199         {\r
200             if( ap instanceof COSStream )\r
201             {\r
202                 COSStream aux = (COSStream) ap;\r
203                 ap = new COSDictionary();\r
204                 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );\r
205             }\r
206             COSDictionary map = (COSDictionary)ap;\r
207             Map<String, PDAppearanceStream> actuals =\r
208                 new HashMap<String, PDAppearanceStream>();\r
209             retval = new COSDictionaryMap( actuals, map );\r
210             for( COSName asName : map.keySet() )\r
211             {\r
212                 COSStream as = (COSStream)map.getDictionaryObject( asName );\r
213                 actuals.put( asName.getName(), new PDAppearanceStream( as ) );\r
214             }\r
215         }\r
216 \r
217         return retval;\r
218     }\r
219 \r
220     /**\r
221      * This will set a list of appearances.  If you would like to set the single\r
222      * appearance then you should use the key "default", and when the PDF is written\r
223      * back to the filesystem then there will only be one stream.\r
224      *\r
225      * @param appearanceMap The updated map with the appearance.\r
226      */\r
227     public void setDownAppearance( Map appearanceMap )\r
228     {\r
229         dictionary.setItem( COSName.getPDFName( "D" ), COSDictionaryMap.convert( appearanceMap ) );\r
230     }\r
231 }\r