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