001: /*
002: * Copyright 2004 by Paulo Soares.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047:
048: package com.lowagie.text.pdf;
049:
050: import java.util.ArrayList;
051:
052: /**
053: * An optional content group is a dictionary representing a collection of graphics
054: * that can be made visible or invisible dynamically by users of viewer applications.
055: * In iText they are referenced as layers.
056: *
057: * @author Paulo Soares (psoares@consiste.pt)
058: */
059: public class PdfLayer extends PdfDictionary implements PdfOCG {
060: protected PdfIndirectReference ref;
061: protected ArrayList children;
062: protected PdfLayer parent;
063: protected String title;
064:
065: /**
066: * Holds value of property on.
067: */
068: private boolean on = true;
069:
070: /**
071: * Holds value of property onPanel.
072: */
073: private boolean onPanel = true;
074:
075: PdfLayer(String title) {
076: this .title = title;
077: }
078:
079: /**
080: * Creates a title layer. A title layer is not really a layer but a collection of layers
081: * under the same title heading.
082: * @param title the title text
083: * @param writer the <CODE>PdfWriter</CODE>
084: * @return the title layer
085: */
086: public static PdfLayer createTitle(String title, PdfWriter writer) {
087: if (title == null)
088: throw new NullPointerException("Title cannot be null.");
089: PdfLayer layer = new PdfLayer(title);
090: writer.registerLayer(layer);
091: return layer;
092: }
093:
094: /**
095: * Creates a new layer.
096: * @param name the name of the layer
097: * @param writer the writer
098: */
099: public PdfLayer(String name, PdfWriter writer) {
100: super (PdfName.OCG);
101: setName(name);
102: ref = writer.getPdfIndirectReference();
103: writer.registerLayer(this );
104: }
105:
106: String getTitle() {
107: return title;
108: }
109:
110: /**
111: * Adds a child layer. Nested layers can only have one parent.
112: * @param child the child layer
113: */
114: public void addChild(PdfLayer child) {
115: if (child.parent != null)
116: throw new IllegalArgumentException("The layer '"
117: + ((PdfString) child.get(PdfName.NAME))
118: .toUnicodeString()
119: + "' already has a parent.");
120: child.parent = this ;
121: if (children == null)
122: children = new ArrayList();
123: children.add(child);
124: }
125:
126: /**
127: * Gets the parent layer.
128: * @return the parent layer or <CODE>null</CODE> if the layer has no parent
129: */
130: public PdfLayer getParent() {
131: return parent;
132: }
133:
134: /**
135: * Gets the children layers.
136: * @return the children layers or <CODE>null</CODE> if the layer has no children
137: */
138: public ArrayList getChildren() {
139: return children;
140: }
141:
142: /**
143: * Gets the <CODE>PdfIndirectReference</CODE> that represents this layer.
144: * @return the <CODE>PdfIndirectReference</CODE> that represents this layer
145: */
146: public PdfIndirectReference getRef() {
147: return ref;
148: }
149:
150: /**
151: * Sets the name of this layer.
152: * @param name the name of this layer
153: */
154: public void setName(String name) {
155: put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE));
156: }
157:
158: /**
159: * Gets the dictionary representing the layer. It just returns <CODE>this</CODE>.
160: * @return the dictionary representing the layer
161: */
162: public PdfObject getPdfObject() {
163: return this ;
164: }
165:
166: /**
167: * Gets the initial visibility of the layer.
168: * @return the initial visibility of the layer
169: */
170: public boolean isOn() {
171: return this .on;
172: }
173:
174: /**
175: * Sets the initial visibility of the layer.
176: * @param on the initial visibility of the layer
177: */
178: public void setOn(boolean on) {
179: this .on = on;
180: }
181:
182: private PdfDictionary getUsage() {
183: PdfDictionary usage = (PdfDictionary) get(PdfName.USAGE);
184: if (usage == null) {
185: usage = new PdfDictionary();
186: put(PdfName.USAGE, usage);
187: }
188: return usage;
189: }
190:
191: /**
192: * Used by the creating application to store application-specific
193: * data associated with this optional content group.
194: * @param creator a text string specifying the application that created the group
195: * @param subtype a string defining the type of content controlled by the group. Suggested
196: * values include but are not limited to <B>Artwork</B>, for graphic-design or publishing
197: * applications, and <B>Technical</B>, for technical designs such as building plans or
198: * schematics
199: */
200: public void setCreatorInfo(String creator, String subtype) {
201: PdfDictionary usage = getUsage();
202: PdfDictionary dic = new PdfDictionary();
203: dic.put(PdfName.CREATOR, new PdfString(creator,
204: PdfObject.TEXT_UNICODE));
205: dic.put(PdfName.SUBTYPE, new PdfName(subtype));
206: usage.put(PdfName.CREATORINFO, dic);
207: }
208:
209: /**
210: * Specifies the language of the content controlled by this
211: * optional content group
212: * @param lang a language string which specifies a language and possibly a locale
213: * (for example, <B>es-MX</B> represents Mexican Spanish)
214: * @param preferred used by viewer applications when there is a partial match but no exact
215: * match between the system language and the language strings in all usage dictionaries
216: */
217: public void setLanguage(String lang, boolean preferred) {
218: PdfDictionary usage = getUsage();
219: PdfDictionary dic = new PdfDictionary();
220: dic.put(PdfName.LANG, new PdfString(lang,
221: PdfObject.TEXT_UNICODE));
222: if (preferred)
223: dic.put(PdfName.PREFERRED, PdfName.ON);
224: usage.put(PdfName.LANGUAGE, dic);
225: }
226:
227: /**
228: * Specifies the recommended state for content in this
229: * group when the document (or part of it) is saved by a viewer application to a format
230: * that does not support optional content (for example, an earlier version of
231: * PDF or a raster image format).
232: * @param export the export state
233: */
234: public void setExport(boolean export) {
235: PdfDictionary usage = getUsage();
236: PdfDictionary dic = new PdfDictionary();
237: dic.put(PdfName.EXPORTSTATE, export ? PdfName.ON : PdfName.OFF);
238: usage.put(PdfName.EXPORT, dic);
239: }
240:
241: /**
242: * Specifies a range of magnifications at which the content
243: * in this optional content group is best viewed.
244: * @param min the minimum recommended magnification factors at which the group
245: * should be ON. A negative value will set the default to 0
246: * @param max the maximum recommended magnification factor at which the group
247: * should be ON. A negative value will set the largest possible magnification supported by the
248: * viewer application
249: */
250: public void setZoom(float min, float max) {
251: if (min <= 0 && max < 0)
252: return;
253: PdfDictionary usage = getUsage();
254: PdfDictionary dic = new PdfDictionary();
255: if (min > 0)
256: dic.put(PdfName.MIN, new PdfNumber(min));
257: if (max >= 0)
258: dic.put(PdfName.MAX, new PdfNumber(max));
259: usage.put(PdfName.ZOOM, dic);
260: }
261:
262: /**
263: * Specifies that the content in this group is intended for
264: * use in printing
265: * @param subtype a name specifying the kind of content controlled by the group;
266: * for example, <B>Trapping</B>, <B>PrintersMarks</B> and <B>Watermark</B>
267: * @param printstate indicates that the group should be
268: * set to that state when the document is printed from a viewer application
269: */
270: public void setPrint(String subtype, boolean printstate) {
271: PdfDictionary usage = getUsage();
272: PdfDictionary dic = new PdfDictionary();
273: dic.put(PdfName.SUBTYPE, new PdfName(subtype));
274: dic.put(PdfName.PRINTSTATE, printstate ? PdfName.ON
275: : PdfName.OFF);
276: usage.put(PdfName.PRINT, dic);
277: }
278:
279: /**
280: * Indicates that the group should be set to that state when the
281: * document is opened in a viewer application.
282: * @param view the view state
283: */
284: public void setView(boolean view) {
285: PdfDictionary usage = getUsage();
286: PdfDictionary dic = new PdfDictionary();
287: dic.put(PdfName.VIEWSTATE, view ? PdfName.ON : PdfName.OFF);
288: usage.put(PdfName.VIEW, dic);
289: }
290:
291: /**
292: * Gets the layer visibility in Acrobat's layer panel
293: * @return the layer visibility in Acrobat's layer panel
294: */
295: public boolean isOnPanel() {
296: return this .onPanel;
297: }
298:
299: /**
300: * Sets the visibility of the layer in Acrobat's layer panel. If <CODE>false</CODE>
301: * the layer cannot be directly manipulated by the user. Note that any children layers will
302: * also be absent from the panel.
303: * @param onPanel the visibility of the layer in Acrobat's layer panel
304: */
305: public void setOnPanel(boolean onPanel) {
306: this.onPanel = onPanel;
307: }
308:
309: }
|