/src/com/itextpdf/text/pdf/events/PdfPageEventForwarder.java

https://github.com/8maki/pdfunlock · Java · 284 lines · 75 code · 17 blank · 192 comment · 11 complexity · 8378c938d13c81db0167cdc7bc8a84ee MD5 · raw file

  1. /*
  2. * $Id: PdfPageEventForwarder.java 4784 2011-03-15 08:33:00Z blowagie $
  3. *
  4. * This file is part of the iText (R) project.
  5. * Copyright (c) 1998-2011 1T3XT BVBA
  6. * Authors: Bruno Lowagie, Paulo Soares, et al.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License version 3
  10. * as published by the Free Software Foundation with the addition of the
  11. * following permission added to Section 15 as permitted in Section 7(a):
  12. * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
  13. * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU Affero General Public License for more details.
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program; if not, see http://www.gnu.org/licenses or write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  23. * http://itextpdf.com/terms-of-use/
  24. *
  25. * The interactive user interfaces in modified source and object code versions
  26. * of this program must display Appropriate Legal Notices, as required under
  27. * Section 5 of the GNU Affero General Public License.
  28. *
  29. * In accordance with Section 7(b) of the GNU Affero General Public License,
  30. * a covered work must retain the producer line in every PDF that is created
  31. * or manipulated using iText.
  32. *
  33. * You can be released from the requirements of the license by purchasing
  34. * a commercial license. Buying such a license is mandatory as soon as you
  35. * develop commercial activities involving the iText software without
  36. * disclosing the source code of your own applications.
  37. * These activities include: offering paid services to customers as an ASP,
  38. * serving PDFs on the fly in a web application, shipping iText with a closed
  39. * source product.
  40. *
  41. * For more information, please contact iText Software Corp. at this
  42. * address: sales@itextpdf.com
  43. */
  44. package com.itextpdf.text.pdf.events;
  45. import java.util.ArrayList;
  46. import com.itextpdf.text.Document;
  47. import com.itextpdf.text.Paragraph;
  48. import com.itextpdf.text.Rectangle;
  49. import com.itextpdf.text.pdf.PdfPageEvent;
  50. import com.itextpdf.text.pdf.PdfWriter;
  51. /**
  52. * If you want to add more than one page event to a PdfWriter,
  53. * you have to construct a PdfPageEventForwarder, add the
  54. * different events to this object and add the forwarder to
  55. * the PdfWriter.
  56. */
  57. public class PdfPageEventForwarder implements PdfPageEvent {
  58. /** ArrayList containing all the PageEvents that have to be executed. */
  59. protected ArrayList<PdfPageEvent> events = new ArrayList<PdfPageEvent>();
  60. /**
  61. * Add a page event to the forwarder.
  62. * @param event an event that has to be added to the forwarder.
  63. */
  64. public void addPageEvent(PdfPageEvent event) {
  65. events.add(event);
  66. }
  67. /**
  68. * Called when the document is opened.
  69. *
  70. * @param writer
  71. * the <CODE>PdfWriter</CODE> for this document
  72. * @param document
  73. * the document
  74. */
  75. public void onOpenDocument(PdfWriter writer, Document document) {
  76. for (PdfPageEvent event: events) {
  77. event.onOpenDocument(writer, document);
  78. }
  79. }
  80. /**
  81. * Called when a page is initialized.
  82. * <P>
  83. * Note that if even if a page is not written this method is still called.
  84. * It is preferable to use <CODE>onEndPage</CODE> to avoid infinite loops.
  85. *
  86. * @param writer
  87. * the <CODE>PdfWriter</CODE> for this document
  88. * @param document
  89. * the document
  90. */
  91. public void onStartPage(PdfWriter writer, Document document) {
  92. for (PdfPageEvent event: events) {
  93. event.onStartPage(writer, document);
  94. }
  95. }
  96. /**
  97. * Called when a page is finished, just before being written to the
  98. * document.
  99. *
  100. * @param writer
  101. * the <CODE>PdfWriter</CODE> for this document
  102. * @param document
  103. * the document
  104. */
  105. public void onEndPage(PdfWriter writer, Document document) {
  106. for (PdfPageEvent event: events) {
  107. event.onEndPage(writer, document);
  108. }
  109. }
  110. /**
  111. * Called when the document is closed.
  112. * <P>
  113. * Note that this method is called with the page number equal to the last
  114. * page plus one.
  115. *
  116. * @param writer
  117. * the <CODE>PdfWriter</CODE> for this document
  118. * @param document
  119. * the document
  120. */
  121. public void onCloseDocument(PdfWriter writer, Document document) {
  122. for (PdfPageEvent event: events) {
  123. event.onCloseDocument(writer, document);
  124. }
  125. }
  126. /**
  127. * Called when a Paragraph is written.
  128. * <P>
  129. * <CODE>paragraphPosition</CODE> will hold the height at which the
  130. * paragraph will be written to. This is useful to insert bookmarks with
  131. * more control.
  132. *
  133. * @param writer
  134. * the <CODE>PdfWriter</CODE> for this document
  135. * @param document
  136. * the document
  137. * @param paragraphPosition
  138. * the position the paragraph will be written to
  139. */
  140. public void onParagraph(PdfWriter writer, Document document,
  141. float paragraphPosition) {
  142. for (PdfPageEvent event: events) {
  143. event.onParagraph(writer, document, paragraphPosition);
  144. }
  145. }
  146. /**
  147. * Called when a Paragraph is written.
  148. * <P>
  149. * <CODE>paragraphPosition</CODE> will hold the height of the end of the
  150. * paragraph.
  151. *
  152. * @param writer
  153. * the <CODE>PdfWriter</CODE> for this document
  154. * @param document
  155. * the document
  156. * @param paragraphPosition
  157. * the position of the end of the paragraph
  158. */
  159. public void onParagraphEnd(PdfWriter writer, Document document,
  160. float paragraphPosition) {
  161. for (PdfPageEvent event: events) {
  162. event.onParagraphEnd(writer, document, paragraphPosition);
  163. }
  164. }
  165. /**
  166. * Called when a Chapter is written.
  167. * <P>
  168. * <CODE>position</CODE> will hold the height at which the chapter will be
  169. * written to.
  170. *
  171. * @param writer
  172. * the <CODE>PdfWriter</CODE> for this document
  173. * @param document
  174. * the document
  175. * @param paragraphPosition
  176. * the position the chapter will be written to
  177. * @param title
  178. * the title of the Chapter
  179. */
  180. public void onChapter(PdfWriter writer, Document document,
  181. float paragraphPosition, Paragraph title) {
  182. for (PdfPageEvent event: events) {
  183. event.onChapter(writer, document, paragraphPosition, title);
  184. }
  185. }
  186. /**
  187. * Called when the end of a Chapter is reached.
  188. * <P>
  189. * <CODE>position</CODE> will hold the height of the end of the chapter.
  190. *
  191. * @param writer
  192. * the <CODE>PdfWriter</CODE> for this document
  193. * @param document
  194. * the document
  195. * @param position
  196. * the position of the end of the chapter.
  197. */
  198. public void onChapterEnd(PdfWriter writer, Document document, float position) {
  199. for (PdfPageEvent event: events) {
  200. event.onChapterEnd(writer, document, position);
  201. }
  202. }
  203. /**
  204. * Called when a Section is written.
  205. * <P>
  206. * <CODE>position</CODE> will hold the height at which the section will be
  207. * written to.
  208. *
  209. * @param writer
  210. * the <CODE>PdfWriter</CODE> for this document
  211. * @param document
  212. * the document
  213. * @param paragraphPosition
  214. * the position the section will be written to
  215. * @param depth
  216. * the number depth of the Section
  217. * @param title
  218. * the title of the section
  219. */
  220. public void onSection(PdfWriter writer, Document document,
  221. float paragraphPosition, int depth, Paragraph title) {
  222. for (PdfPageEvent event: events) {
  223. event.onSection(writer, document, paragraphPosition, depth, title);
  224. }
  225. }
  226. /**
  227. * Called when the end of a Section is reached.
  228. * <P>
  229. * <CODE>position</CODE> will hold the height of the section end.
  230. *
  231. * @param writer
  232. * the <CODE>PdfWriter</CODE> for this document
  233. * @param document
  234. * the document
  235. * @param position
  236. * the position of the end of the section
  237. */
  238. public void onSectionEnd(PdfWriter writer, Document document, float position) {
  239. for (PdfPageEvent event: events) {
  240. event.onSectionEnd(writer, document, position);
  241. }
  242. }
  243. /**
  244. * Called when a <CODE>Chunk</CODE> with a generic tag is written.
  245. * <P>
  246. * It is useful to pinpoint the <CODE>Chunk</CODE> location to generate
  247. * bookmarks, for example.
  248. *
  249. * @param writer
  250. * the <CODE>PdfWriter</CODE> for this document
  251. * @param document
  252. * the document
  253. * @param rect
  254. * the <CODE>Rectangle</CODE> containing the <CODE>Chunk
  255. * </CODE>
  256. * @param text
  257. * the text of the tag
  258. */
  259. public void onGenericTag(PdfWriter writer, Document document,
  260. Rectangle rect, String text) {
  261. PdfPageEvent event;
  262. for (Object element : events) {
  263. event = (PdfPageEvent)element;
  264. event.onGenericTag(writer, document, rect, text);
  265. }
  266. }
  267. }