001/* 002 * $Id: PdfPageEventForwarder.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf.events; 045 046import java.util.ArrayList; 047 048import com.itextpdf.text.Document; 049import com.itextpdf.text.Paragraph; 050import com.itextpdf.text.Rectangle; 051import com.itextpdf.text.pdf.PdfPageEvent; 052import com.itextpdf.text.pdf.PdfWriter; 053 054/** 055 * If you want to add more than one page event to a PdfWriter, 056 * you have to construct a PdfPageEventForwarder, add the 057 * different events to this object and add the forwarder to 058 * the PdfWriter. 059 */ 060 061public class PdfPageEventForwarder implements PdfPageEvent { 062 063 /** ArrayList containing all the PageEvents that have to be executed. */ 064 protected ArrayList<PdfPageEvent> events = new ArrayList<PdfPageEvent>(); 065 066 /** 067 * Add a page event to the forwarder. 068 * @param event an event that has to be added to the forwarder. 069 */ 070 public void addPageEvent(PdfPageEvent event) { 071 events.add(event); 072 } 073 074 /** 075 * Called when the document is opened. 076 * 077 * @param writer 078 * the <CODE>PdfWriter</CODE> for this document 079 * @param document 080 * the document 081 */ 082 public void onOpenDocument(PdfWriter writer, Document document) { 083 for (PdfPageEvent event: events) { 084 event.onOpenDocument(writer, document); 085 } 086 } 087 088 /** 089 * Called when a page is initialized. 090 * <P> 091 * Note that if even if a page is not written this method is still called. 092 * It is preferable to use <CODE>onEndPage</CODE> to avoid infinite loops. 093 * 094 * @param writer 095 * the <CODE>PdfWriter</CODE> for this document 096 * @param document 097 * the document 098 */ 099 public void onStartPage(PdfWriter writer, Document document) { 100 for (PdfPageEvent event: events) { 101 event.onStartPage(writer, document); 102 } 103 } 104 105 /** 106 * Called when a page is finished, just before being written to the 107 * document. 108 * 109 * @param writer 110 * the <CODE>PdfWriter</CODE> for this document 111 * @param document 112 * the document 113 */ 114 public void onEndPage(PdfWriter writer, Document document) { 115 for (PdfPageEvent event: events) { 116 event.onEndPage(writer, document); 117 } 118 } 119 120 /** 121 * Called when the document is closed. 122 * <P> 123 * Note that this method is called with the page number equal to the last 124 * page plus one. 125 * 126 * @param writer 127 * the <CODE>PdfWriter</CODE> for this document 128 * @param document 129 * the document 130 */ 131 public void onCloseDocument(PdfWriter writer, Document document) { 132 for (PdfPageEvent event: events) { 133 event.onCloseDocument(writer, document); 134 } 135 } 136 137 /** 138 * Called when a Paragraph is written. 139 * <P> 140 * <CODE>paragraphPosition</CODE> will hold the height at which the 141 * paragraph will be written to. This is useful to insert bookmarks with 142 * more control. 143 * 144 * @param writer 145 * the <CODE>PdfWriter</CODE> for this document 146 * @param document 147 * the document 148 * @param paragraphPosition 149 * the position the paragraph will be written to 150 */ 151 public void onParagraph(PdfWriter writer, Document document, 152 float paragraphPosition) { 153 for (PdfPageEvent event: events) { 154 event.onParagraph(writer, document, paragraphPosition); 155 } 156 } 157 158 /** 159 * Called when a Paragraph is written. 160 * <P> 161 * <CODE>paragraphPosition</CODE> will hold the height of the end of the 162 * paragraph. 163 * 164 * @param writer 165 * the <CODE>PdfWriter</CODE> for this document 166 * @param document 167 * the document 168 * @param paragraphPosition 169 * the position of the end of the paragraph 170 */ 171 public void onParagraphEnd(PdfWriter writer, Document document, 172 float paragraphPosition) { 173 for (PdfPageEvent event: events) { 174 event.onParagraphEnd(writer, document, paragraphPosition); 175 } 176 } 177 178 /** 179 * Called when a Chapter is written. 180 * <P> 181 * <CODE>position</CODE> will hold the height at which the chapter will be 182 * written to. 183 * 184 * @param writer 185 * the <CODE>PdfWriter</CODE> for this document 186 * @param document 187 * the document 188 * @param paragraphPosition 189 * the position the chapter will be written to 190 * @param title 191 * the title of the Chapter 192 */ 193 public void onChapter(PdfWriter writer, Document document, 194 float paragraphPosition, Paragraph title) { 195 for (PdfPageEvent event: events) { 196 event.onChapter(writer, document, paragraphPosition, title); 197 } 198 } 199 200 /** 201 * Called when the end of a Chapter is reached. 202 * <P> 203 * <CODE>position</CODE> will hold the height of the end of the chapter. 204 * 205 * @param writer 206 * the <CODE>PdfWriter</CODE> for this document 207 * @param document 208 * the document 209 * @param position 210 * the position of the end of the chapter. 211 */ 212 public void onChapterEnd(PdfWriter writer, Document document, float position) { 213 for (PdfPageEvent event: events) { 214 event.onChapterEnd(writer, document, position); 215 } 216 } 217 218 /** 219 * Called when a Section is written. 220 * <P> 221 * <CODE>position</CODE> will hold the height at which the section will be 222 * written to. 223 * 224 * @param writer 225 * the <CODE>PdfWriter</CODE> for this document 226 * @param document 227 * the document 228 * @param paragraphPosition 229 * the position the section will be written to 230 * @param depth 231 * the number depth of the Section 232 * @param title 233 * the title of the section 234 */ 235 public void onSection(PdfWriter writer, Document document, 236 float paragraphPosition, int depth, Paragraph title) { 237 for (PdfPageEvent event: events) { 238 event.onSection(writer, document, paragraphPosition, depth, title); 239 } 240 } 241 242 /** 243 * Called when the end of a Section is reached. 244 * <P> 245 * <CODE>position</CODE> will hold the height of the section end. 246 * 247 * @param writer 248 * the <CODE>PdfWriter</CODE> for this document 249 * @param document 250 * the document 251 * @param position 252 * the position of the end of the section 253 */ 254 public void onSectionEnd(PdfWriter writer, Document document, float position) { 255 for (PdfPageEvent event: events) { 256 event.onSectionEnd(writer, document, position); 257 } 258 } 259 260 /** 261 * Called when a <CODE>Chunk</CODE> with a generic tag is written. 262 * <P> 263 * It is useful to pinpoint the <CODE>Chunk</CODE> location to generate 264 * bookmarks, for example. 265 * 266 * @param writer 267 * the <CODE>PdfWriter</CODE> for this document 268 * @param document 269 * the document 270 * @param rect 271 * the <CODE>Rectangle</CODE> containing the <CODE>Chunk 272 * </CODE> 273 * @param text 274 * the text of the tag 275 */ 276 public void onGenericTag(PdfWriter writer, Document document, 277 Rectangle rect, String text) { 278 PdfPageEvent event; 279 for (Object element : events) { 280 event = (PdfPageEvent)element; 281 event.onGenericTag(writer, document, rect, text); 282 } 283 } 284}