KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfTemplate


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

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

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

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

95     
96     PdfTemplate(PdfWriter wr) {
97         super(wr);
98         type = TYPE_TEMPLATE;
99         pageResources = new PageResources();
100         pageResources.addDefaultColor(wr.getDefaultColorspace());
101         thisReference = 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, float width, float height) {
118         return createTemplate(writer, width, height, null);
119     }
120     
121     static PdfTemplate createTemplate(PdfWriter writer, float width, float height, PdfName forcedName) {
122         PdfTemplate template = new PdfTemplate(writer);
123         template.setWidth(width);
124         template.setHeight(height);
125         writer.addDirectTemplateSimple(template, forcedName);
126         return template;
127     }
128
129     /**
130      * Sets the bounding width of this template.
131      *
132      * @param width the bounding width
133      */

134     
135     public void setWidth(float width) {
136         bBox.setLeft(0);
137         bBox.setRight(width);
138     }
139     
140     /**
141      * Sets the bounding heigth of this template.
142      *
143      * @param height the bounding height
144      */

145     
146     public void setHeight(float height) {
147         bBox.setBottom(0);
148         bBox.setTop(height);
149     }
150     
151     /**
152      * Gets the bounding width of this template.
153      *
154      * @return width the bounding width
155      */

156     public float getWidth() {
157         return bBox.getWidth();
158     }
159     
160     /**
161      * Gets the bounding heigth of this template.
162      *
163      * @return heigth the bounding height
164      */

165     
166     public float getHeight() {
167         return bBox.getHeight();
168     }
169     
170     public Rectangle getBoundingBox() {
171         return bBox;
172     }
173     
174     public void setBoundingBox(Rectangle bBox) {
175         this.bBox = bBox;
176     }
177     
178     /**
179      * Sets the layer this template belongs to.
180      * @param layer the layer this template belongs to
181      */

182     public void setLayer(PdfOCG layer) {
183         this.layer = layer;
184     }
185     
186     /**
187      * Gets the layer this template belongs to.
188      * @return the layer this template belongs to or <code>null</code> for no layer defined
189      */

190     public PdfOCG getLayer() {
191         return layer;
192     }
193
194     public void setMatrix(float a, float b, float c, float d, float e, float f) {
195         matrix = new PdfArray();
196         matrix.add(new PdfNumber(a));
197         matrix.add(new PdfNumber(b));
198         matrix.add(new PdfNumber(c));
199         matrix.add(new PdfNumber(d));
200         matrix.add(new PdfNumber(e));
201         matrix.add(new PdfNumber(f));
202     }
203
204     PdfArray getMatrix() {
205         return matrix;
206     }
207     
208     /**
209      * Gets the indirect reference to this template.
210      *
211      * @return the indirect reference to this template
212      */

213     
214     public PdfIndirectReference getIndirectReference() {
215         return thisReference;
216     }
217         
218     public void beginVariableText() {
219         content.append("/Tx BMC ");
220     }
221     
222     public void endVariableText() {
223         content.append("EMC ");
224     }
225     
226     /**
227      * Constructs the resources used by this template.
228      *
229      * @return the resources used by this template
230      */

231     
232     PdfObject getResources() {
233         return getPageResources().getResources();
234     }
235     
236     /**
237      * Gets the stream representing this template.
238      *
239      * @return the stream representing this template
240      */

241     
242     PdfStream getFormXObject() throws IOException JavaDoc {
243         return new PdfFormXObject(this);
244     }
245         
246     /**
247      * Gets a duplicate of this <CODE>PdfTemplate</CODE>. All
248      * the members are copied by reference but the buffer stays different.
249      * @return a copy of this <CODE>PdfTemplate</CODE>
250      */

251     
252     public PdfContentByte getDuplicate() {
253         PdfTemplate tpl = new PdfTemplate();
254         tpl.writer = writer;
255         tpl.pdf = pdf;
256         tpl.thisReference = thisReference;
257         tpl.pageResources = pageResources;
258         tpl.bBox = new Rectangle(bBox);
259         tpl.group = group;
260         tpl.layer = layer;
261         if (matrix != null) {
262             tpl.matrix = new PdfArray(matrix);
263         }
264         tpl.separator = separator;
265         return tpl;
266     }
267     
268     public int getType() {
269         return type;
270     }
271     
272     PageResources getPageResources() {
273         return pageResources;
274     }
275     
276     /** Getter for property group.
277      * @return Value of property group.
278      *
279      */

280     public PdfTransparencyGroup getGroup() {
281         return this.group;
282     }
283     
284     /** Setter for property group.
285      * @param group New value of property group.
286      *
287      */

288     public void setGroup(PdfTransparencyGroup group) {
289         this.group = group;
290     }
291     
292 }
Popular Tags