01: package dinamica;
02:
03: import com.lowagie.text.*;
04: import com.lowagie.text.pdf.*;
05:
06: /**
07: * PDF reports utility class.<br>
08: * Generic class to intercept IText page events and
09: * print custom footer text and Page X of Y using IText
10: * template object.<br>
11: * Footer looks like:<br>
12: * [FooterText - DateTime] ...... [X of Y]
13: * Creation date: 2006-12-28<br>
14: * (c) 2006 Martin Cordova<br>
15: * This code is released under the LGPL license<br>
16: * Dinamica Framework - http://www.martincordova.com<br>
17: * @author Martin Cordova (martin.cordova@gmail.com)
18: */
19: public class PDFPageEvents extends PdfPageEventHelper {
20:
21: String footerLeft = "";
22: String footerRight = "";
23: String pageXofY = "";
24: PdfTemplate tpl = null;
25: BaseFont bf = null;
26: PdfContentByte cb = null;
27:
28: /**
29: * Initializes object with required parameters
30: * @param footerLeft Text for letf-side footer
31: * @param pageXofY Text between "X" and "Y" numbers, might be " of " or " de ", according to language
32: * @param tpl Template used to print "X of Y" right-side footer
33: * @param bf Base font
34: * @param cb Content byte
35: * @param reportDateTime Formatted date/time for left-side footer
36: * @throws Throwable
37: */
38: public PDFPageEvents(String footerLeft, String pageXofY,
39: PdfTemplate tpl, BaseFont bf, PdfContentByte cb,
40: String reportDateTime) throws Throwable {
41: this .pageXofY = pageXofY;
42: this .tpl = tpl;
43: this .bf = bf;
44: this .cb = cb;
45: this .footerLeft = footerLeft + " - " + reportDateTime;
46: }
47:
48: /**
49: * Print value on empty template (total number of pages), this
50: * will update footer on every page where the template was inserted
51: */
52: public void onCloseDocument(PdfWriter writer, Document document) {
53: // print total number of pages into template
54: // this will update footer on every page
55: int pageNum = writer.getPageNumber() - 1;
56: tpl.beginText();
57: tpl.setFontAndSize(bf, 10);
58: tpl.showText(String.valueOf(pageNum));
59: tpl.endText();
60: }
61:
62: /**
63: * Create custom-made footer on every page, this
64: * will place the empty template on every page
65: */
66: public void onEndPage(PdfWriter writer, Document document) {
67:
68: // print (x of ...) on every page (bottom right)
69: // append template at the end of the footer, like:
70: // 1 of [template] - template is an empty box
71: String footer = writer.getPageNumber() + this .pageXofY;
72: float tWidth = bf.getWidthPoint(footer, 10);
73: float extraSpace = bf.getWidthPoint("00", 10);
74: float ty = document.bottom() - 12; //below bottom margin
75: float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer
76:
77: // print "X of " on right-side and footer + date on left side
78: cb.beginText();
79: cb.setFontAndSize(bf, 10);
80: cb
81: .showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx,
82: ty, 0);
83: cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footerLeft,
84: document.left(), ty, 0);
85: cb.endText();
86:
87: // now append empty template after "X of "
88: cb.addTemplate(tpl, document.right() - extraSpace, ty);
89:
90: }
91:
92: }
|