001: /*
002: * $Id: PdfTemplate.java 2752 2007-05-15 14:58:33Z blowagie $
003: * $Name$
004: *
005: * Copyright 2001, 2002 Paulo Soares
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.pdf;
052:
053: import java.io.IOException;
054:
055: import com.lowagie.text.Rectangle;
056:
057: /**
058: * Implements the form XObject.
059: */
060:
061: public class PdfTemplate extends PdfContentByte {
062: public static final int TYPE_TEMPLATE = 1;
063: public static final int TYPE_IMPORTED = 2;
064: public static final int TYPE_PATTERN = 3;
065: protected int type;
066: /** The indirect reference to this template */
067: protected PdfIndirectReference this Reference;
068:
069: /** The resources used by this template */
070: protected PageResources pageResources;
071:
072: /** The bounding box of this template */
073: protected Rectangle bBox = new Rectangle(0, 0);
074:
075: protected PdfArray matrix;
076:
077: protected PdfTransparencyGroup group;
078:
079: protected PdfOCG layer;
080:
081: /**
082: *Creates a <CODE>PdfTemplate</CODE>.
083: */
084:
085: protected PdfTemplate() {
086: super (null);
087: type = TYPE_TEMPLATE;
088: }
089:
090: /**
091: * Creates new PdfTemplate
092: *
093: * @param wr the <CODE>PdfWriter</CODE>
094: */
095:
096: PdfTemplate(PdfWriter wr) {
097: super (wr);
098: type = TYPE_TEMPLATE;
099: pageResources = new PageResources();
100: pageResources.addDefaultColor(wr.getDefaultColorspace());
101: this Reference = writer.getPdfIndirectReference();
102: }
103:
104: /**
105: * Creates a new template.
106: * <P>
107: * Creates a new template that is nothing more than a form XObject. This template can be included
108: * in this template or in another template. Templates are only written
109: * to the output when the document is closed permitting things like showing text in the first page
110: * that is only defined in the last page.
111: *
112: * @param writer the PdfWriter to use
113: * @param width the bounding box width
114: * @param height the bounding box height
115: * @return the templated created
116: */
117: public static PdfTemplate createTemplate(PdfWriter writer,
118: float width, float height) {
119: return createTemplate(writer, width, height, null);
120: }
121:
122: static PdfTemplate createTemplate(PdfWriter writer, float width,
123: float height, PdfName forcedName) {
124: PdfTemplate template = new PdfTemplate(writer);
125: template.setWidth(width);
126: template.setHeight(height);
127: writer.addDirectTemplateSimple(template, forcedName);
128: return template;
129: }
130:
131: /**
132: * Sets the bounding width of this template.
133: *
134: * @param width the bounding width
135: */
136:
137: public void setWidth(float width) {
138: bBox.setLeft(0);
139: bBox.setRight(width);
140: }
141:
142: /**
143: * Sets the bounding heigth of this template.
144: *
145: * @param height the bounding height
146: */
147:
148: public void setHeight(float height) {
149: bBox.setBottom(0);
150: bBox.setTop(height);
151: }
152:
153: /**
154: * Gets the bounding width of this template.
155: *
156: * @return width the bounding width
157: */
158: public float getWidth() {
159: return bBox.getWidth();
160: }
161:
162: /**
163: * Gets the bounding heigth of this template.
164: *
165: * @return heigth the bounding height
166: */
167:
168: public float getHeight() {
169: return bBox.getHeight();
170: }
171:
172: public Rectangle getBoundingBox() {
173: return bBox;
174: }
175:
176: public void setBoundingBox(Rectangle bBox) {
177: this .bBox = bBox;
178: }
179:
180: /**
181: * Sets the layer this template belongs to.
182: * @param layer the layer this template belongs to
183: */
184: public void setLayer(PdfOCG layer) {
185: this .layer = layer;
186: }
187:
188: /**
189: * Gets the layer this template belongs to.
190: * @return the layer this template belongs to or <code>null</code> for no layer defined
191: */
192: public PdfOCG getLayer() {
193: return layer;
194: }
195:
196: public void setMatrix(float a, float b, float c, float d, float e,
197: float f) {
198: matrix = new PdfArray();
199: matrix.add(new PdfNumber(a));
200: matrix.add(new PdfNumber(b));
201: matrix.add(new PdfNumber(c));
202: matrix.add(new PdfNumber(d));
203: matrix.add(new PdfNumber(e));
204: matrix.add(new PdfNumber(f));
205: }
206:
207: PdfArray getMatrix() {
208: return matrix;
209: }
210:
211: /**
212: * Gets the indirect reference to this template.
213: *
214: * @return the indirect reference to this template
215: */
216:
217: public PdfIndirectReference getIndirectReference() {
218: return this Reference;
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: * @return the stream representing this template
243: */
244:
245: PdfStream getFormXObject() throws IOException {
246: return new PdfFormXObject(this );
247: }
248:
249: /**
250: * Gets a duplicate of this <CODE>PdfTemplate</CODE>. All
251: * the members are copied by reference but the buffer stays different.
252: * @return a copy of this <CODE>PdfTemplate</CODE>
253: */
254:
255: public PdfContentByte getDuplicate() {
256: PdfTemplate tpl = new PdfTemplate();
257: tpl.writer = writer;
258: tpl.pdf = pdf;
259: tpl.this Reference = this Reference;
260: tpl.pageResources = pageResources;
261: tpl.bBox = new Rectangle(bBox);
262: tpl.group = group;
263: tpl.layer = layer;
264: if (matrix != null) {
265: tpl.matrix = new PdfArray(matrix);
266: }
267: tpl.separator = separator;
268: return tpl;
269: }
270:
271: public int getType() {
272: return type;
273: }
274:
275: PageResources getPageResources() {
276: return pageResources;
277: }
278:
279: /** Getter for property group.
280: * @return Value of property group.
281: *
282: */
283: public PdfTransparencyGroup getGroup() {
284: return this .group;
285: }
286:
287: /** Setter for property group.
288: * @param group New value of property group.
289: *
290: */
291: public void setGroup(PdfTransparencyGroup group) {
292: this.group = group;
293: }
294:
295: }
|