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

49
50 package com.lowagie.text.pdf;
51 import java.io.IOException;
52
53 import com.lowagie.text.Rectangle;
54
55 /**
56  * Implements the form XObject.
57  */

58
59 public class PdfTemplate extends PdfContentByte {
60     public static final int TYPE_TEMPLATE = 1;
61     public static final int TYPE_IMPORTED = 2;
62     public static final int TYPE_PATTERN = 3;
63     protected int type;
64     /** The indirect reference to this template */
65     protected PdfIndirectReference thisReference;
66     
67     /** The resources used by this template */
68     protected PageResources pageResources;
69     
70     
71     /** The bounding box of this template */
72     protected Rectangle bBox = new Rectangle(0, 0);
73     
74     protected PdfArray matrix;
75     
76     protected PdfTransparencyGroup group;
77     
78     protected PdfOCG layer;
79     
80     /**
81      *Creates a <CODE>PdfTemplate</CODE>.
82      */

83     
84     protected PdfTemplate() {
85         super(null);
86         type = TYPE_TEMPLATE;
87     }
88     
89     /**
90      * Creates new PdfTemplate
91      *
92      * @param wr the <CODE>PdfWriter</CODE>
93      */

94     
95     PdfTemplate(PdfWriter wr) {
96         super(wr);
97         type = TYPE_TEMPLATE;
98         pageResources = new PageResources();
99         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         return tpl;
270     }
271     
272     public int getType() {
273         return type;
274     }
275     
276     PageResources getPageResources() {
277         return pageResources;
278     }
279     
280     /** Getter for property group.
281      * @return Value of property group.
282      *
283      */

284     public PdfTransparencyGroup getGroup() {
285         return this.group;
286     }
287     
288     /** Setter for property group.
289      * @param group New value of property group.
290      *
291      */

292     public void setGroup(PdfTransparencyGroup group) {
293         this.group = group;
294     }
295     
296 }