001: /*
002: * $Id: RtfHeaderFooter.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.headerfooter;
052:
053: import java.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056:
057: import com.lowagie.text.DocumentException;
058: import com.lowagie.text.Element;
059: import com.lowagie.text.HeaderFooter;
060: import com.lowagie.text.Image;
061: import com.lowagie.text.Paragraph;
062: import com.lowagie.text.Phrase;
063: import com.lowagie.text.Table;
064: import com.lowagie.text.rtf.RtfBasicElement;
065: import com.lowagie.text.rtf.document.RtfDocument;
066: import com.lowagie.text.rtf.field.RtfPageNumber;
067:
068: /**
069: * The RtfHeaderFooter represents one header or footer. This class can be used
070: * directly.
071: *
072: * @version $Id: RtfHeaderFooter.java 2776 2007-05-23 20:01:40Z hallm $
073: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
074: * @author Thomas Bickel (tmb99@inode.at)
075: */
076: public class RtfHeaderFooter extends HeaderFooter implements
077: RtfBasicElement {
078:
079: /**
080: * Constant for the header type
081: */
082: public static final int TYPE_HEADER = 1;
083: /**
084: * Constant for the footer type
085: */
086: public static final int TYPE_FOOTER = 2;
087: /**
088: * Constant for displaying the header/footer on the first page
089: */
090: public static final int DISPLAY_FIRST_PAGE = 0;
091: /**
092: * Constant for displaying the header/footer on all pages
093: */
094: public static final int DISPLAY_ALL_PAGES = 1;
095: /**
096: * Constant for displaying the header/footer on all left hand pages
097: */
098: public static final int DISPLAY_LEFT_PAGES = 2;
099: /**
100: * Constant for displaying the header/footer on all right hand pages
101: */
102: public static final int DISPLAY_RIGHT_PAGES = 4;
103:
104: /**
105: * Constant for a header on all pages
106: */
107: private static final byte[] HEADER_ALL = "\\header".getBytes();
108: /**
109: * Constant for a header on the first page
110: */
111: private static final byte[] HEADER_FIRST = "\\headerf".getBytes();
112: /**
113: * Constant for a header on all left hand pages
114: */
115: private static final byte[] HEADER_LEFT = "\\headerl".getBytes();
116: /**
117: * Constant for a header on all right hand pages
118: */
119: private static final byte[] HEADER_RIGHT = "\\headerr".getBytes();
120: /**
121: * Constant for a footer on all pages
122: */
123: private static final byte[] FOOTER_ALL = "\\footer".getBytes();
124: /**
125: * Constant for a footer on the first page
126: */
127: private static final byte[] FOOTER_FIRST = "\\footerf".getBytes();
128: /**
129: * Constnat for a footer on the left hand pages
130: */
131: private static final byte[] FOOTER_LEFT = "\\footerl".getBytes();
132: /**
133: * Constant for a footer on the right hand pages
134: */
135: private static final byte[] FOOTER_RIGHT = "\\footerr".getBytes();
136:
137: /**
138: * The RtfDocument this RtfHeaderFooter belongs to
139: */
140: private RtfDocument document = null;
141: /**
142: * The content of this RtfHeaderFooter
143: */
144: private Object[] content = null;
145: /**
146: * The display type of this RtfHeaderFooter. TYPE_HEADER or TYPE_FOOTER
147: */
148: private int type = TYPE_HEADER;
149: /**
150: * The display location of this RtfHeaderFooter. DISPLAY_FIRST_PAGE,
151: * DISPLAY_LEFT_PAGES, DISPLAY_RIGHT_PAGES or DISPLAY_ALL_PAGES
152: */
153: private int displayAt = DISPLAY_ALL_PAGES;
154:
155: /**
156: * Constructs a RtfHeaderFooter based on a HeaderFooter with a certain type and displayAt
157: * location. For internal use only.
158: *
159: * @param doc The RtfDocument this RtfHeaderFooter belongs to
160: * @param headerFooter The HeaderFooter to base this RtfHeaderFooter on
161: * @param type The type of RtfHeaderFooter
162: * @param displayAt The display location of this RtfHeaderFooter
163: */
164: protected RtfHeaderFooter(RtfDocument doc,
165: HeaderFooter headerFooter, int type, int displayAt) {
166: super (new Phrase(""), false);
167: this .document = doc;
168: this .type = type;
169: this .displayAt = displayAt;
170: Paragraph par = new Paragraph();
171: par.setAlignment(headerFooter.alignment());
172: if (headerFooter.getBefore() != null) {
173: par.add(headerFooter.getBefore());
174: }
175: if (headerFooter.isNumbered()) {
176: par.add(new RtfPageNumber(this .document));
177: }
178: if (headerFooter.getAfter() != null) {
179: par.add(headerFooter.getAfter());
180: }
181: try {
182: this .content = new Object[1];
183: if (this .document != null) {
184: this .content[0] = this .document.getMapper().mapElement(
185: par);
186: ((RtfBasicElement) this .content[0]).setInHeader(true);
187: } else {
188: this .content[0] = par;
189: }
190: } catch (DocumentException de) {
191: de.printStackTrace();
192: }
193: }
194:
195: /**
196: * Constructs a RtfHeaderFooter as a copy of an existing RtfHeaderFooter.
197: * For internal use only.
198: *
199: * @param doc The RtfDocument this RtfHeaderFooter belongs to
200: * @param headerFooter The RtfHeaderFooter to copy
201: * @param displayAt The display location of this RtfHeaderFooter
202: */
203: protected RtfHeaderFooter(RtfDocument doc,
204: RtfHeaderFooter headerFooter, int displayAt) {
205: super (new Phrase(""), false);
206: this .document = doc;
207: this .content = headerFooter.getContent();
208: this .displayAt = displayAt;
209: for (int i = 0; i < this .content.length; i++) {
210: if (this .content[i] instanceof Element) {
211: try {
212: this .content[i] = this .document.getMapper()
213: .mapElement((Element) this .content[i]);
214: } catch (DocumentException de) {
215: de.printStackTrace();
216: }
217: }
218: if (this .content[i] instanceof RtfBasicElement) {
219: ((RtfBasicElement) this .content[i]).setInHeader(true);
220: }
221: }
222: }
223:
224: /**
225: * Constructs a RtfHeaderFooter for a HeaderFooter.
226: *
227: * @param doc The RtfDocument this RtfHeaderFooter belongs to
228: * @param headerFooter The HeaderFooter to base this RtfHeaderFooter on
229: */
230: protected RtfHeaderFooter(RtfDocument doc, HeaderFooter headerFooter) {
231: super (new Phrase(""), false);
232: this .document = doc;
233: Paragraph par = new Paragraph();
234: par.setAlignment(headerFooter.alignment());
235: if (headerFooter.getBefore() != null) {
236: par.add(headerFooter.getBefore());
237: }
238: if (headerFooter.isNumbered()) {
239: par.add(new RtfPageNumber(this .document));
240: }
241: if (headerFooter.getAfter() != null) {
242: par.add(headerFooter.getAfter());
243: }
244: try {
245: this .content = new Object[1];
246: this .content[0] = doc.getMapper().mapElement(par);
247: ((RtfBasicElement) this .content[0]).setInHeader(true);
248: } catch (DocumentException de) {
249: de.printStackTrace();
250: }
251: }
252:
253: /**
254: * Constructs a RtfHeaderFooter for any Element.
255: *
256: * @param element The Element to display as content of this RtfHeaderFooter
257: */
258: public RtfHeaderFooter(Element element) {
259: this (new Element[] { element });
260: }
261:
262: /**
263: * Constructs a RtfHeaderFooter for an array of Elements.
264: *
265: * @param elements The Elements to display as the content of this RtfHeaderFooter.
266: */
267: public RtfHeaderFooter(Element[] elements) {
268: super (new Phrase(""), false);
269: this .content = new Object[elements.length];
270: for (int i = 0; i < elements.length; i++) {
271: this .content[i] = elements[i];
272: }
273: }
274:
275: /**
276: * Sets the RtfDocument this RtfElement belongs to
277: *
278: * @param doc The RtfDocument to use
279: */
280: public void setRtfDocument(RtfDocument doc) {
281: this .document = doc;
282: if (this .document != null) {
283: for (int i = 0; i < this .content.length; i++) {
284: try {
285: if (this .content[i] instanceof Element) {
286: this .content[i] = this .document.getMapper()
287: .mapElement((Element) this .content[i]);
288: ((RtfBasicElement) this .content[i])
289: .setInHeader(true);
290: } else if (this .content[i] instanceof RtfBasicElement) {
291: ((RtfBasicElement) this .content[i])
292: .setRtfDocument(this .document);
293: ((RtfBasicElement) this .content[i])
294: .setInHeader(true);
295: }
296: } catch (DocumentException de) {
297: de.printStackTrace();
298: }
299: }
300: }
301: }
302:
303: /**
304: * Writes the content of this RtfHeaderFooter
305: *
306: * @return A byte array with the content of this RtfHeaderFooter
307: * @deprecated replaced by {@link #writeContent(OutputStream)}
308: */
309: public byte[] write() {
310: ByteArrayOutputStream result = new ByteArrayOutputStream();
311: try {
312: writeContent(result);
313: } catch (IOException ioe) {
314: ioe.printStackTrace();
315: }
316: return result.toByteArray();
317: }
318:
319: /**
320: * Writes the content of this RtfHeaderFooter
321: */
322: public void writeContent(final OutputStream result)
323: throws IOException {
324: result.write(OPEN_GROUP);
325: if (this .type == TYPE_HEADER) {
326: if (this .displayAt == DISPLAY_ALL_PAGES) {
327: result.write(HEADER_ALL);
328: } else if (this .displayAt == DISPLAY_FIRST_PAGE) {
329: result.write(HEADER_FIRST);
330: } else if (this .displayAt == DISPLAY_LEFT_PAGES) {
331: result.write(HEADER_LEFT);
332: } else if (this .displayAt == DISPLAY_RIGHT_PAGES) {
333: result.write(HEADER_RIGHT);
334: }
335: } else {
336: if (this .displayAt == DISPLAY_ALL_PAGES) {
337: result.write(FOOTER_ALL);
338: } else if (this .displayAt == DISPLAY_FIRST_PAGE) {
339: result.write(FOOTER_FIRST);
340: } else if (this .displayAt == DISPLAY_LEFT_PAGES) {
341: result.write(FOOTER_LEFT);
342: } else if (this .displayAt == DISPLAY_RIGHT_PAGES) {
343: result.write(FOOTER_RIGHT);
344: }
345: }
346: result.write(DELIMITER);
347: for (int i = 0; i < this .content.length; i++) {
348: if (this .content[i] instanceof RtfBasicElement) {
349: //result.write(((RtfBasicElement) this.content[i]).write());
350: RtfBasicElement rbe = (RtfBasicElement) this .content[i];
351: rbe.writeContent(result);
352: }
353: }
354: result.write(CLOSE_GROUP);
355: }
356:
357: /**
358: * Sets the display location of this RtfHeaderFooter
359: *
360: * @param displayAt The display location to use.
361: */
362: public void setDisplayAt(int displayAt) {
363: this .displayAt = displayAt;
364: }
365:
366: /**
367: * Sets the type of this RtfHeaderFooter
368: *
369: * @param type The type to use.
370: */
371: public void setType(int type) {
372: this .type = type;
373: }
374:
375: /**
376: * Gets the content of this RtfHeaderFooter
377: *
378: * @return The content of this RtfHeaderFooter
379: */
380: private Object[] getContent() {
381: return this .content;
382: }
383:
384: /**
385: * Unused
386: * @param inTable
387: */
388: public void setInTable(boolean inTable) {
389: }
390:
391: /**
392: * Unused
393: * @param inHeader
394: */
395: public void setInHeader(boolean inHeader) {
396: }
397:
398: /**
399: * Set the alignment of this RtfHeaderFooter. Passes the setting
400: * on to the contained element.
401: */
402: public void setAlignment(int alignment) {
403: super .setAlignment(alignment);
404: for (int i = 0; i < this .content.length; i++) {
405: if (this .content[i] instanceof Paragraph) {
406: ((Paragraph) this .content[i]).setAlignment(alignment);
407: } else if (this .content[i] instanceof Table) {
408: ((Table) this .content[i]).setAlignment(alignment);
409: } else if (this .content[i] instanceof Image) {
410: ((Image) this.content[i]).setAlignment(alignment);
411: }
412: }
413: }
414: }
|