KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > events > PdfPageEventForwarder


1 /*
2  * $Id: PdfPageEventForwarder.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2005 Bruno Lowagie
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.events;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.Iterator JavaDoc;
55
56 import com.lowagie.text.Document;
57 import com.lowagie.text.Paragraph;
58 import com.lowagie.text.Rectangle;
59 import com.lowagie.text.pdf.PdfPageEvent;
60 import com.lowagie.text.pdf.PdfWriter;
61
62 /**
63  * If you want to add more than one page event to a PdfWriter,
64  * you have to construct a PdfPageEventForwarder, add the
65  * different events to this object and add the forwarder to
66  * the PdfWriter.
67  */

68
69 public class PdfPageEventForwarder implements PdfPageEvent {
70
71     /** ArrayList containing all the PageEvents that have to be executed. */
72     protected ArrayList JavaDoc events = new ArrayList JavaDoc();
73     
74     /**
75      * Add a page event to the forwarder.
76      * @param event an event that has to be added to the forwarder.
77      */

78     public void addPageEvent(PdfPageEvent event) {
79         events.add(event);
80     }
81     
82     /**
83      * Called when the document is opened.
84      *
85      * @param writer
86      * the <CODE>PdfWriter</CODE> for this document
87      * @param document
88      * the document
89      */

90     public void onOpenDocument(PdfWriter writer, Document document) {
91         PdfPageEvent event;
92         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
93             event = (PdfPageEvent)i.next();
94             event.onOpenDocument(writer, document);
95         }
96     }
97
98     /**
99      * Called when a page is initialized.
100      * <P>
101      * Note that if even if a page is not written this method is still called.
102      * It is preferable to use <CODE>onEndPage</CODE> to avoid infinite loops.
103      *
104      * @param writer
105      * the <CODE>PdfWriter</CODE> for this document
106      * @param document
107      * the document
108      */

109     public void onStartPage(PdfWriter writer, Document document) {
110         PdfPageEvent event;
111         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
112             event = (PdfPageEvent)i.next();
113             event.onStartPage(writer, document);
114         }
115     }
116
117     /**
118      * Called when a page is finished, just before being written to the
119      * document.
120      *
121      * @param writer
122      * the <CODE>PdfWriter</CODE> for this document
123      * @param document
124      * the document
125      */

126     public void onEndPage(PdfWriter writer, Document document) {
127         PdfPageEvent event;
128         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
129             event = (PdfPageEvent)i.next();
130             event.onEndPage(writer, document);
131         }
132     }
133
134     /**
135      * Called when the document is closed.
136      * <P>
137      * Note that this method is called with the page number equal to the last
138      * page plus one.
139      *
140      * @param writer
141      * the <CODE>PdfWriter</CODE> for this document
142      * @param document
143      * the document
144      */

145     public void onCloseDocument(PdfWriter writer, Document document) {
146         PdfPageEvent event;
147         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
148             event = (PdfPageEvent)i.next();
149             event.onCloseDocument(writer, document);
150         }
151     }
152
153     /**
154      * Called when a Paragraph is written.
155      * <P>
156      * <CODE>paragraphPosition</CODE> will hold the height at which the
157      * paragraph will be written to. This is useful to insert bookmarks with
158      * more control.
159      *
160      * @param writer
161      * the <CODE>PdfWriter</CODE> for this document
162      * @param document
163      * the document
164      * @param paragraphPosition
165      * the position the paragraph will be written to
166      */

167     public void onParagraph(PdfWriter writer, Document document,
168             float paragraphPosition) {
169         PdfPageEvent event;
170         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
171             event = (PdfPageEvent)i.next();
172             event.onParagraph(writer, document, paragraphPosition);
173         }
174     }
175
176     /**
177      * Called when a Paragraph is written.
178      * <P>
179      * <CODE>paragraphPosition</CODE> will hold the height of the end of the
180      * paragraph.
181      *
182      * @param writer
183      * the <CODE>PdfWriter</CODE> for this document
184      * @param document
185      * the document
186      * @param paragraphPosition
187      * the position of the end of the paragraph
188      */

189     public void onParagraphEnd(PdfWriter writer, Document document,
190             float paragraphPosition) {
191         PdfPageEvent event;
192         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
193             event = (PdfPageEvent)i.next();
194             event.onParagraphEnd(writer, document, paragraphPosition);
195         }
196     }
197
198     /**
199      * Called when a Chapter is written.
200      * <P>
201      * <CODE>position</CODE> will hold the height at which the chapter will be
202      * written to.
203      *
204      * @param writer
205      * the <CODE>PdfWriter</CODE> for this document
206      * @param document
207      * the document
208      * @param paragraphPosition
209      * the position the chapter will be written to
210      * @param title
211      * the title of the Chapter
212      */

213     public void onChapter(PdfWriter writer, Document document,
214             float paragraphPosition, Paragraph title) {
215         PdfPageEvent event;
216         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
217             event = (PdfPageEvent)i.next();
218             event.onChapter(writer, document, paragraphPosition, title);
219         }
220     }
221
222     /**
223      * Called when the end of a Chapter is reached.
224      * <P>
225      * <CODE>position</CODE> will hold the height of the end of the chapter.
226      *
227      * @param writer
228      * the <CODE>PdfWriter</CODE> for this document
229      * @param document
230      * the document
231      * @param position
232      * the position of the end of the chapter.
233      */

234     public void onChapterEnd(PdfWriter writer, Document document, float position) {
235         PdfPageEvent event;
236         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
237             event = (PdfPageEvent)i.next();
238             event.onChapterEnd(writer, document, position);
239         }
240     }
241
242     /**
243      * Called when a Section is written.
244      * <P>
245      * <CODE>position</CODE> will hold the height at which the section will be
246      * written to.
247      *
248      * @param writer
249      * the <CODE>PdfWriter</CODE> for this document
250      * @param document
251      * the document
252      * @param paragraphPosition
253      * the position the section will be written to
254      * @param depth
255      * the number depth of the Section
256      * @param title
257      * the title of the section
258      */

259     public void onSection(PdfWriter writer, Document document,
260             float paragraphPosition, int depth, Paragraph title) {
261         PdfPageEvent event;
262         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
263             event = (PdfPageEvent)i.next();
264             event.onSection(writer, document, paragraphPosition, depth, title);
265         }
266     }
267
268     /**
269      * Called when the end of a Section is reached.
270      * <P>
271      * <CODE>position</CODE> will hold the height of the section end.
272      *
273      * @param writer
274      * the <CODE>PdfWriter</CODE> for this document
275      * @param document
276      * the document
277      * @param position
278      * the position of the end of the section
279      */

280     public void onSectionEnd(PdfWriter writer, Document document, float position) {
281         PdfPageEvent event;
282         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
283             event = (PdfPageEvent)i.next();
284             event.onSectionEnd(writer, document, position);
285         }
286     }
287
288     /**
289      * Called when a <CODE>Chunk</CODE> with a generic tag is written.
290      * <P>
291      * It is usefull to pinpoint the <CODE>Chunk</CODE> location to generate
292      * bookmarks, for example.
293      *
294      * @param writer
295      * the <CODE>PdfWriter</CODE> for this document
296      * @param document
297      * the document
298      * @param rect
299      * the <CODE>Rectangle</CODE> containing the <CODE>Chunk
300      * </CODE>
301      * @param text
302      * the text of the tag
303      */

304     public void onGenericTag(PdfWriter writer, Document document,
305             Rectangle rect, String JavaDoc text) {
306         PdfPageEvent event;
307         for (Iterator JavaDoc i = events.iterator(); i.hasNext(); ) {
308             event = (PdfPageEvent)i.next();
309             event.onGenericTag(writer, document, rect, text);
310         }
311     }
312 }
Popular Tags