KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfLayer


1 /*
2  * Copyright 2004 by Paulo Soares.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47
48 package com.lowagie.text.pdf;
49
50 import java.util.ArrayList JavaDoc;
51 /**
52  * An optional content group is a dictionary representing a collection of graphics
53  * that can be made visible or invisible dynamically by users of viewer applications.
54  * In iText they are referenced as layers.
55  *
56  * @author Paulo Soares (psoares@consiste.pt)
57  */

58 public class PdfLayer extends PdfDictionary implements PdfOCG {
59     protected PdfIndirectReference ref;
60     protected ArrayList JavaDoc children;
61     protected PdfLayer parent;
62     protected String JavaDoc title;
63
64     /**
65      * Holds value of property on.
66      */

67     private boolean on = true;
68     
69     /**
70      * Holds value of property onPanel.
71      */

72     private boolean onPanel = true;
73     
74     PdfLayer(String JavaDoc title) {
75         this.title = title;
76     }
77     
78     /**
79      * Creates a title layer. A title layer is not really a layer but a collection of layers
80      * under the same title heading.
81      * @param title the title text
82      * @param writer the <CODE>PdfWriter</CODE>
83      * @return the title layer
84      */

85     public static PdfLayer createTitle(String JavaDoc title, PdfWriter writer) {
86         if (title == null)
87             throw new NullPointerException JavaDoc("Title cannot be null.");
88         PdfLayer layer = new PdfLayer(title);
89         writer.registerLayer(layer);
90         return layer;
91     }
92     /**
93      * Creates a new layer.
94      * @param name the name of the layer
95      * @param writer the writer
96      */

97     public PdfLayer(String JavaDoc name, PdfWriter writer) {
98         super(PdfName.OCG);
99         setName(name);
100         ref = writer.getPdfIndirectReference();
101         writer.registerLayer(this);
102     }
103     
104     String JavaDoc getTitle() {
105         return title;
106     }
107     
108     /**
109      * Adds a child layer. Nested layers can only have one parent.
110      * @param child the child layer
111      */

112     public void addChild(PdfLayer child) {
113         if (child.parent != null)
114             throw new IllegalArgumentException JavaDoc("The layer '" + ((PdfString)child.get(PdfName.NAME)).toUnicodeString() + "' already has a parent.");
115         child.parent = this;
116         if (children == null)
117             children = new ArrayList JavaDoc();
118         children.add(child);
119     }
120
121     
122     /**
123      * Gets the parent layer.
124      * @return the parent layer or <CODE>null</CODE> if the layer has no parent
125      */

126     public PdfLayer getParent() {
127         return parent;
128     }
129     
130     /**
131      * Gets the children layers.
132      * @return the children layers or <CODE>null</CODE> if the layer has no children
133      */

134     public ArrayList JavaDoc getChildren() {
135         return children;
136     }
137     
138     /**
139      * Gets the <CODE>PdfIndirectReference</CODE> that represents this layer.
140      * @return the <CODE>PdfIndirectReference</CODE> that represents this layer
141      */

142     public PdfIndirectReference getRef() {
143         return ref;
144     }
145     
146     /**
147      * Sets the name of this layer.
148      * @param name the name of this layer
149      */

150     public void setName(String JavaDoc name) {
151         put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE));
152     }
153     
154     /**
155      * Gets the dictionary representing the layer. It just returns <CODE>this</CODE>.
156      * @return the dictionary representing the layer
157      */

158     public PdfObject getPdfObject() {
159         return this;
160     }
161     
162     /**
163      * Gets the initial visibility of the layer.
164      * @return the initial visibility of the layer
165      */

166     public boolean isOn() {
167         return this.on;
168     }
169     
170     /**
171      * Sets the initial visibility of the layer.
172      * @param on the initial visibility of the layer
173      */

174     public void setOn(boolean on) {
175         this.on = on;
176     }
177     
178     private PdfDictionary getUsage() {
179         PdfDictionary usage = (PdfDictionary)get(PdfName.USAGE);
180         if (usage == null) {
181             usage = new PdfDictionary();
182             put(PdfName.USAGE, usage);
183         }
184         return usage;
185     }
186     
187     /**
188      * Used by the creating application to store application-specific
189      * data associated with this optional content group.
190      * @param creator a text string specifying the application that created the group
191      * @param subtype a string defining the type of content controlled by the group. Suggested
192      * values include but are not limited to <B>Artwork</B>, for graphic-design or publishing
193      * applications, and <B>Technical</B>, for technical designs such as building plans or
194      * schematics
195      */

196     public void setCreatorInfo(String JavaDoc creator, String JavaDoc subtype) {
197         PdfDictionary usage = getUsage();
198         PdfDictionary dic = new PdfDictionary();
199         dic.put(PdfName.CREATOR, new PdfString(creator, PdfObject.TEXT_UNICODE));
200         dic.put(PdfName.SUBTYPE, new PdfName(subtype));
201         usage.put(PdfName.CREATORINFO, dic);
202     }
203     
204     /**
205      * Specifies the language of the content controlled by this
206      * optional content group
207      * @param lang a language string which specifies a language and possibly a locale
208      * (for example, <B>es-MX</B> represents Mexican Spanish)
209      * @param preferred used by viewer applications when there is a partial match but no exact
210      * match between the system language and the language strings in all usage dictionaries
211      */

212     public void setLanguage(String JavaDoc lang, boolean preferred) {
213         PdfDictionary usage = getUsage();
214         PdfDictionary dic = new PdfDictionary();
215         dic.put(PdfName.LANG, new PdfString(lang, PdfObject.TEXT_UNICODE));
216         if (preferred)
217             dic.put(PdfName.PREFERRED, PdfName.ON);
218         usage.put(PdfName.LANGUAGE, dic);
219     }
220     
221     /**
222      * Specifies the recommended state for content in this
223      * group when the document (or part of it) is saved by a viewer application to a format
224      * that does not support optional content (for example, an earlier version of
225      * PDF or a raster image format).
226      * @param export the export state
227      */

228     public void setExport(boolean export) {
229         PdfDictionary usage = getUsage();
230         PdfDictionary dic = new PdfDictionary();
231         dic.put(PdfName.EXPORTSTATE, export ? PdfName.ON : PdfName.OFF);
232         usage.put(PdfName.EXPORT, dic);
233     }
234     
235     /**
236      * Specifies a range of magnifications at which the content
237      * in this optional content group is best viewed.
238      * @param min the minimum recommended magnification factors at which the group
239      * should be ON. A negative value will set the default to 0
240      * @param max the maximum recommended magnification factor at which the group
241      * should be ON. A negative value will set the largest possible magnification supported by the
242      * viewer application
243      */

244     public void setZoom(float min, float max) {
245         if (min <= 0 && max < 0)
246             return;
247         PdfDictionary usage = getUsage();
248         PdfDictionary dic = new PdfDictionary();
249         if (min > 0)
250             dic.put(PdfName.MIN, new PdfNumber(min));
251         if (max >= 0)
252             dic.put(PdfName.MAX, new PdfNumber(max));
253         usage.put(PdfName.ZOOM, dic);
254     }
255
256     /**
257      * Specifies that the content in this group is intended for
258      * use in printing
259      * @param subtype a name specifying the kind of content controlled by the group;
260      * for example, <B>Trapping</B>, <B>PrintersMarks</B> and <B>Watermark</B>
261      * @param printstate indicates that the group should be
262      * set to that state when the document is printed from a viewer application
263      */

264     public void setPrint(String JavaDoc subtype, boolean printstate) {
265         PdfDictionary usage = getUsage();
266         PdfDictionary dic = new PdfDictionary();
267         dic.put(PdfName.SUBTYPE, new PdfName(subtype));
268         dic.put(PdfName.PRINTSTATE, printstate ? PdfName.ON : PdfName.OFF);
269         usage.put(PdfName.PRINT, dic);
270     }
271
272     /**
273      * Indicates that the group should be set to that state when the
274      * document is opened in a viewer application.
275      * @param view the view state
276      */

277     public void setView(boolean view) {
278         PdfDictionary usage = getUsage();
279         PdfDictionary dic = new PdfDictionary();
280         dic.put(PdfName.VIEWSTATE, view ? PdfName.ON : PdfName.OFF);
281         usage.put(PdfName.VIEW, dic);
282     }
283     
284     /**
285      * Gets the layer visibility in Acrobat's layer panel
286      * @return the layer visibility in Acrobat's layer panel
287      */

288     public boolean isOnPanel() {
289         return this.onPanel;
290     }
291     
292     /**
293      * Sets the visibility of the layer in Acrobat's layer panel. If <CODE>false</CODE>
294      * the layer cannot be directly manipulated by the user. Note that any children layers will
295      * also be absent from the panel.
296      * @param onPanel the visibility of the layer in Acrobat's layer panel
297      */

298     public void setOnPanel(boolean onPanel) {
299         this.onPanel = onPanel;
300     }
301     
302 }
303
Popular Tags