001/* 002 * $Id: PdfLayer.java 4892 2011-06-02 06:45:07Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf; 045 046import java.io.IOException; 047import java.util.ArrayList; 048 049import com.itextpdf.text.error_messages.MessageLocalization; 050/** 051 * An optional content group is a dictionary representing a collection of graphics 052 * that can be made visible or invisible dynamically by users of viewer applications. 053 * In iText they are referenced as layers. 054 * 055 * @author Paulo Soares 056 */ 057public class PdfLayer extends PdfDictionary implements PdfOCG { 058 protected PdfIndirectReference ref; 059 protected ArrayList<PdfLayer> children; 060 protected PdfLayer parent; 061 protected String title; 062 063 /** 064 * Holds value of property on. 065 */ 066 private boolean on = true; 067 068 /** 069 * Holds value of property onPanel. 070 */ 071 private boolean onPanel = true; 072 073 PdfLayer(String title) { 074 this.title = title; 075 } 076 077 /** 078 * Creates a title layer. A title layer is not really a layer but a collection of layers 079 * under the same title heading. 080 * @param title the title text 081 * @param writer the <CODE>PdfWriter</CODE> 082 * @return the title layer 083 */ 084 public static PdfLayer createTitle(String title, PdfWriter writer) { 085 if (title == null) 086 throw new NullPointerException(MessageLocalization.getComposedMessage("title.cannot.be.null")); 087 PdfLayer layer = new PdfLayer(title); 088 writer.registerLayer(layer); 089 return layer; 090 } 091 /** 092 * Creates a new layer. 093 * @param name the name of the layer 094 * @param writer the writer 095 * @throws IOException 096 */ 097 public PdfLayer(String name, PdfWriter writer) throws IOException { 098 super(PdfName.OCG); 099 setName(name); 100 if (writer instanceof PdfStamperImp) 101 ref = writer.addToBody(this).getIndirectReference(); 102 else 103 ref = writer.getPdfIndirectReference(); 104 writer.registerLayer(this); 105 } 106 107 String getTitle() { 108 return title; 109 } 110 111 /** 112 * Adds a child layer. Nested layers can only have one parent. 113 * @param child the child layer 114 */ 115 public void addChild(PdfLayer child) { 116 if (child.parent != null) 117 throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.layer.1.already.has.a.parent", child.getAsString(PdfName.NAME).toUnicodeString())); 118 child.parent = this; 119 if (children == null) 120 children = new ArrayList<PdfLayer>(); 121 children.add(child); 122 } 123 124 125 /** 126 * Gets the parent layer. 127 * @return the parent layer or <CODE>null</CODE> if the layer has no parent 128 */ 129 public PdfLayer getParent() { 130 return parent; 131 } 132 133 /** 134 * Gets the children layers. 135 * @return the children layers or <CODE>null</CODE> if the layer has no children 136 */ 137 public ArrayList<PdfLayer> getChildren() { 138 return children; 139 } 140 141 /** 142 * Gets the <CODE>PdfIndirectReference</CODE> that represents this layer. 143 * @return the <CODE>PdfIndirectReference</CODE> that represents this layer 144 */ 145 public PdfIndirectReference getRef() { 146 return ref; 147 } 148 149 /** 150 * Sets the <CODE>PdfIndirectReference</CODE> that represents this layer. 151 * This can only be done from PdfStamperImp. 152 * @param ref The reference to the OCG object 153 * @since 2.1.2 154 */ 155 void setRef(PdfIndirectReference ref) { 156 this.ref = ref; 157 } 158 159 /** 160 * Sets the name of this layer. 161 * @param name the name of this layer 162 */ 163 public void setName(String name) { 164 put(PdfName.NAME, new PdfString(name, PdfObject.TEXT_UNICODE)); 165 } 166 167 /** 168 * Gets the dictionary representing the layer. It just returns <CODE>this</CODE>. 169 * @return the dictionary representing the layer 170 */ 171 public PdfObject getPdfObject() { 172 return this; 173 } 174 175 /** 176 * Gets the initial visibility of the layer. 177 * @return the initial visibility of the layer 178 */ 179 public boolean isOn() { 180 return this.on; 181 } 182 183 /** 184 * Sets the initial visibility of the layer. 185 * @param on the initial visibility of the layer 186 */ 187 public void setOn(boolean on) { 188 this.on = on; 189 } 190 191 private PdfDictionary getUsage() { 192 PdfDictionary usage = getAsDict(PdfName.USAGE); 193 if (usage == null) { 194 usage = new PdfDictionary(); 195 put(PdfName.USAGE, usage); 196 } 197 return usage; 198 } 199 200 /** 201 * Used by the creating application to store application-specific 202 * data associated with this optional content group. 203 * @param creator a text string specifying the application that created the group 204 * @param subtype a string defining the type of content controlled by the group. Suggested 205 * values include but are not limited to <B>Artwork</B>, for graphic-design or publishing 206 * applications, and <B>Technical</B>, for technical designs such as building plans or 207 * schematics 208 */ 209 public void setCreatorInfo(String creator, String subtype) { 210 PdfDictionary usage = getUsage(); 211 PdfDictionary dic = new PdfDictionary(); 212 dic.put(PdfName.CREATOR, new PdfString(creator, PdfObject.TEXT_UNICODE)); 213 dic.put(PdfName.SUBTYPE, new PdfName(subtype)); 214 usage.put(PdfName.CREATORINFO, dic); 215 } 216 217 /** 218 * Specifies the language of the content controlled by this 219 * optional content group 220 * @param lang a language string which specifies a language and possibly a locale 221 * (for example, <B>es-MX</B> represents Mexican Spanish) 222 * @param preferred used by viewer applications when there is a partial match but no exact 223 * match between the system language and the language strings in all usage dictionaries 224 */ 225 public void setLanguage(String lang, boolean preferred) { 226 PdfDictionary usage = getUsage(); 227 PdfDictionary dic = new PdfDictionary(); 228 dic.put(PdfName.LANG, new PdfString(lang, PdfObject.TEXT_UNICODE)); 229 if (preferred) 230 dic.put(PdfName.PREFERRED, PdfName.ON); 231 usage.put(PdfName.LANGUAGE, dic); 232 } 233 234 /** 235 * Specifies the recommended state for content in this 236 * group when the document (or part of it) is saved by a viewer application to a format 237 * that does not support optional content (for example, an earlier version of 238 * PDF or a raster image format). 239 * @param export the export state 240 */ 241 public void setExport(boolean export) { 242 PdfDictionary usage = getUsage(); 243 PdfDictionary dic = new PdfDictionary(); 244 dic.put(PdfName.EXPORTSTATE, export ? PdfName.ON : PdfName.OFF); 245 usage.put(PdfName.EXPORT, dic); 246 } 247 248 /** 249 * Specifies a range of magnifications at which the content 250 * in this optional content group is best viewed. 251 * @param min the minimum recommended magnification factors at which the group 252 * should be ON. A negative value will set the default to 0 253 * @param max the maximum recommended magnification factor at which the group 254 * should be ON. A negative value will set the largest possible magnification supported by the 255 * viewer application 256 */ 257 public void setZoom(float min, float max) { 258 if (min <= 0 && max < 0) 259 return; 260 PdfDictionary usage = getUsage(); 261 PdfDictionary dic = new PdfDictionary(); 262 if (min > 0) 263 dic.put(PdfName.MIN_LOWER_CASE, new PdfNumber(min)); 264 if (max >= 0) 265 dic.put(PdfName.MAX_LOWER_CASE, new PdfNumber(max)); 266 usage.put(PdfName.ZOOM, dic); 267 } 268 269 /** 270 * Specifies that the content in this group is intended for 271 * use in printing 272 * @param subtype a name specifying the kind of content controlled by the group; 273 * for example, <B>Trapping</B>, <B>PrintersMarks</B> and <B>Watermark</B> 274 * @param printstate indicates that the group should be 275 * set to that state when the document is printed from a viewer application 276 */ 277 public void setPrint(String subtype, boolean printstate) { 278 PdfDictionary usage = getUsage(); 279 PdfDictionary dic = new PdfDictionary(); 280 dic.put(PdfName.SUBTYPE, new PdfName(subtype)); 281 dic.put(PdfName.PRINTSTATE, printstate ? PdfName.ON : PdfName.OFF); 282 usage.put(PdfName.PRINT, dic); 283 } 284 285 /** 286 * Indicates that the group should be set to that state when the 287 * document is opened in a viewer application. 288 * @param view the view state 289 */ 290 public void setView(boolean view) { 291 PdfDictionary usage = getUsage(); 292 PdfDictionary dic = new PdfDictionary(); 293 dic.put(PdfName.VIEWSTATE, view ? PdfName.ON : PdfName.OFF); 294 usage.put(PdfName.VIEW, dic); 295 } 296 297 /** 298 * Indicates that the group contains a pagination artifact. 299 * @param pe one of the following names: "HF" (Header Footer), 300 * "FG" (Foreground), "BG" (Background), or "L" (Logo). 301 * @since 5.0.2 302 */ 303 public void setPageElement(String pe) { 304 PdfDictionary usage = getUsage(); 305 PdfDictionary dic = new PdfDictionary(); 306 dic.put(PdfName.SUBTYPE, new PdfName(pe)); 307 usage.put(PdfName.PAGEELEMENT, dic); 308 } 309 310 /** 311 * One of more users for whom this optional content group is primarily intended. 312 * @param type should be "Ind" (Individual), "Ttl" (Title), or "Org" (Organization). 313 * @param names one or more names 314 * @since 5.0.2 315 */ 316 public void setUser(String type, String... names) { 317 PdfDictionary usage = getUsage(); 318 PdfDictionary dic = new PdfDictionary(); 319 dic.put(PdfName.TYPE, new PdfName(type)); 320 PdfArray arr = new PdfArray(); 321 for (String s : names) 322 arr.add(new PdfString(s, PdfObject.TEXT_UNICODE)); 323 usage.put(PdfName.NAME, arr); 324 usage.put(PdfName.USER, dic); 325 } 326 327 /** 328 * Gets the layer visibility in Acrobat's layer panel 329 * @return the layer visibility in Acrobat's layer panel 330 */ 331 public boolean isOnPanel() { 332 return this.onPanel; 333 } 334 335 /** 336 * Sets the visibility of the layer in Acrobat's layer panel. If <CODE>false</CODE> 337 * the layer cannot be directly manipulated by the user. Note that any children layers will 338 * also be absent from the panel. 339 * @param onPanel the visibility of the layer in Acrobat's layer panel 340 */ 341 public void setOnPanel(boolean onPanel) { 342 this.onPanel = onPanel; 343 } 344 345}