001/* 002 * $Id: PdfTemplate.java 4798 2011-04-06 11:45:02Z 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; 045import java.io.IOException; 046 047import com.itextpdf.text.Rectangle; 048 049/** 050 * Implements the form XObject. 051 */ 052 053public class PdfTemplate extends PdfContentByte { 054 public static final int TYPE_TEMPLATE = 1; 055 public static final int TYPE_IMPORTED = 2; 056 public static final int TYPE_PATTERN = 3; 057 protected int type; 058 /** The indirect reference to this template */ 059 protected PdfIndirectReference thisReference; 060 061 /** The resources used by this template */ 062 protected PageResources pageResources; 063 064 065 /** The bounding box of this template */ 066 protected Rectangle bBox = new Rectangle(0, 0); 067 068 protected PdfArray matrix; 069 070 protected PdfTransparencyGroup group; 071 072 protected PdfOCG layer; 073 074 /** 075 * A dictionary with additional information 076 * @since 5.1.0 077 */ 078 private PdfDictionary additional = null; 079 080 /** 081 *Creates a <CODE>PdfTemplate</CODE>. 082 */ 083 084 protected PdfTemplate() { 085 super(null); 086 type = TYPE_TEMPLATE; 087 } 088 089 /** 090 * Creates new PdfTemplate 091 * 092 * @param wr the <CODE>PdfWriter</CODE> 093 */ 094 095 PdfTemplate(PdfWriter wr) { 096 super(wr); 097 type = TYPE_TEMPLATE; 098 pageResources = new PageResources(); 099 pageResources.addDefaultColor(wr.getDefaultColorspace()); 100 thisReference = writer.getPdfIndirectReference(); 101 } 102 103 /** 104 * Creates a new template. 105 * <P> 106 * Creates a new template that is nothing more than a form XObject. This template can be included 107 * in this template or in another template. Templates are only written 108 * to the output when the document is closed permitting things like showing text in the first page 109 * that is only defined in the last page. 110 * 111 * @param writer the PdfWriter to use 112 * @param width the bounding box width 113 * @param height the bounding box height 114 * @return the created template 115 */ 116 public static PdfTemplate createTemplate(PdfWriter writer, float width, float height) { 117 return createTemplate(writer, width, height, null); 118 } 119 120 static PdfTemplate createTemplate(PdfWriter writer, float width, float height, PdfName forcedName) { 121 PdfTemplate template = new PdfTemplate(writer); 122 template.setWidth(width); 123 template.setHeight(height); 124 writer.addDirectTemplateSimple(template, forcedName); 125 return template; 126 } 127 128 /** 129 * Sets the bounding width of this template. 130 * 131 * @param width the bounding width 132 */ 133 134 public void setWidth(float width) { 135 bBox.setLeft(0); 136 bBox.setRight(width); 137 } 138 139 /** 140 * Sets the bounding height of this template. 141 * 142 * @param height the bounding height 143 */ 144 145 public void setHeight(float height) { 146 bBox.setBottom(0); 147 bBox.setTop(height); 148 } 149 150 /** 151 * Gets the bounding width of this template. 152 * 153 * @return width the bounding width 154 */ 155 public float getWidth() { 156 return bBox.getWidth(); 157 } 158 159 /** 160 * Gets the bounding height of this template. 161 * 162 * @return height the bounding height 163 */ 164 165 public float getHeight() { 166 return bBox.getHeight(); 167 } 168 169 public Rectangle getBoundingBox() { 170 return bBox; 171 } 172 173 public void setBoundingBox(Rectangle bBox) { 174 this.bBox = bBox; 175 } 176 177 /** 178 * Sets the layer this template belongs to. 179 * @param layer the layer this template belongs to 180 */ 181 public void setLayer(PdfOCG layer) { 182 this.layer = layer; 183 } 184 185 /** 186 * Gets the layer this template belongs to. 187 * @return the layer this template belongs to or <code>null</code> for no layer defined 188 */ 189 public PdfOCG getLayer() { 190 return layer; 191 } 192 193 public void setMatrix(float a, float b, float c, float d, float e, float f) { 194 matrix = new PdfArray(); 195 matrix.add(new PdfNumber(a)); 196 matrix.add(new PdfNumber(b)); 197 matrix.add(new PdfNumber(c)); 198 matrix.add(new PdfNumber(d)); 199 matrix.add(new PdfNumber(e)); 200 matrix.add(new PdfNumber(f)); 201 } 202 203 PdfArray getMatrix() { 204 return matrix; 205 } 206 207 /** 208 * Gets the indirect reference to this template. 209 * 210 * @return the indirect reference to this template 211 */ 212 213 public PdfIndirectReference getIndirectReference() { 214 // uncomment the null check as soon as we're sure all examples still work 215 if (thisReference == null /* && writer != null */) { 216 thisReference = writer.getPdfIndirectReference(); 217 } 218 return thisReference; 219 } 220 221 public void beginVariableText() { 222 content.append("/Tx BMC "); 223 } 224 225 public void endVariableText() { 226 content.append("EMC "); 227 } 228 229 /** 230 * Constructs the resources used by this template. 231 * 232 * @return the resources used by this template 233 */ 234 235 PdfObject getResources() { 236 return getPageResources().getResources(); 237 } 238 239 /** 240 * Gets the stream representing this template. 241 * 242 * @param compressionLevel the compressionLevel 243 * @return the stream representing this template 244 * @since 2.1.3 (replacing the method without param compressionLevel) 245 */ 246 PdfStream getFormXObject(int compressionLevel) throws IOException { 247 return new PdfFormXObject(this, compressionLevel); 248 } 249 250 /** 251 * Gets a duplicate of this <CODE>PdfTemplate</CODE>. All 252 * the members are copied by reference but the buffer stays different. 253 * @return a copy of this <CODE>PdfTemplate</CODE> 254 */ 255 256 public PdfContentByte getDuplicate() { 257 PdfTemplate tpl = new PdfTemplate(); 258 tpl.writer = writer; 259 tpl.pdf = pdf; 260 tpl.thisReference = thisReference; 261 tpl.pageResources = pageResources; 262 tpl.bBox = new Rectangle(bBox); 263 tpl.group = group; 264 tpl.layer = layer; 265 if (matrix != null) { 266 tpl.matrix = new PdfArray(matrix); 267 } 268 tpl.separator = separator; 269 tpl.additional = additional; 270 return tpl; 271 } 272 273 public int getType() { 274 return type; 275 } 276 277 PageResources getPageResources() { 278 return pageResources; 279 } 280 281 /** Getter for property group. 282 * @return Value of property group. 283 * 284 */ 285 public PdfTransparencyGroup getGroup() { 286 return this.group; 287 } 288 289 /** Setter for property group. 290 * @param group New value of property group. 291 * 292 */ 293 public void setGroup(PdfTransparencyGroup group) { 294 this.group = group; 295 } 296 297 298 /** 299 * Getter for the dictionary with additional information. 300 * 301 * @return a PdfDictionary with additional information. 302 * @since 5.1.0 303 */ 304 public PdfDictionary getAdditional() { 305 return this.additional; 306 } 307 308 /** 309 * Sets a dictionary with extra entries, for instance /Measure. 310 * 311 * @param additional 312 * a PdfDictionary with additional information. 313 * @since 5.1.0 314 */ 315 public void setAdditional(PdfDictionary additional) { 316 this.additional = additional; 317 } 318}