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