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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.pdfbox.pdmodel.interactive.annotation;
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;
24 import org.apache.pdfbox.pdmodel.common.COSObjectable;
25 import org.apache.pdfbox.pdmodel.common.COSDictionaryMap;
27 import java.util.HashMap;
31 * This class represents a PDF /AP entry the appearance dictionary.
33 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
34 * @version $Revision: 1.4 $
36 public class PDAppearanceDictionary implements COSObjectable
38 private COSDictionary dictionary;
43 public PDAppearanceDictionary()
45 dictionary = new COSDictionary();
46 //the N entry is required.
47 dictionary.setItem( COSName.getPDFName( "N" ), new COSDictionary() );
53 * @param dict The annotations dictionary.
55 public PDAppearanceDictionary( COSDictionary dict )
61 * returns the dictionary.
62 * @return the dictionary
64 public COSDictionary getDictionary()
70 * returns the dictionary.
71 * @return the dictionary
73 public COSBase getCOSObject()
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
83 * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
85 public Map getNormalAppearance()
87 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "N" ) );
92 else if( ap instanceof COSStream )
94 COSStream aux = (COSStream) ap;
95 ap = new COSDictionary();
96 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );
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() )
103 COSStream as = (COSStream)map.getDictionaryObject( asName );
104 actuals.put( asName.getName(), new PDAppearanceStream( as ) );
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.
115 * @param appearanceMap The updated map with the appearance.
117 public void setNormalAppearance( Map appearanceMap )
119 dictionary.setItem( COSName.getPDFName( "N" ), COSDictionaryMap.convert( appearanceMap ) );
123 * This will set the normal appearance when there is only one appearance
126 * @param ap The appearance stream to show.
128 public void setNormalAppearance( PDAppearanceStream ap )
130 dictionary.setItem( COSName.getPDFName( "N" ), ap.getStream() );
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.
139 * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
141 public Map getRolloverAppearance()
144 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "R" ) );
147 retval = getNormalAppearance();
151 if( ap instanceof COSStream )
153 COSStream aux = (COSStream) ap;
154 ap = new COSDictionary();
155 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );
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() )
162 COSStream as = (COSStream)map.getDictionaryObject( asName );
163 actuals.put( asName.getName(), new PDAppearanceStream( as ) );
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.
175 * @param appearanceMap The updated map with the appearance.
177 public void setRolloverAppearance( Map appearanceMap )
179 dictionary.setItem( COSName.getPDFName( "R" ), COSDictionaryMap.convert( appearanceMap ) );
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.
188 * @return A list of key(java.lang.String) value(PDAppearanceStream) pairs
190 public Map getDownAppearance()
193 COSBase ap = dictionary.getDictionaryObject( COSName.getPDFName( "D" ) );
196 retval = getNormalAppearance();
200 if( ap instanceof COSStream )
202 COSStream aux = (COSStream) ap;
203 ap = new COSDictionary();
204 ((COSDictionary)ap).setItem(COSName.getPDFName( "default" ), aux );
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() )
212 COSStream as = (COSStream)map.getDictionaryObject( asName );
213 actuals.put( asName.getName(), new PDAppearanceStream( as ) );
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.
225 * @param appearanceMap The updated map with the appearance.
227 public void setDownAppearance( Map appearanceMap )
229 dictionary.setItem( COSName.getPDFName( "D" ), COSDictionaryMap.convert( appearanceMap ) );