KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > reports > PageXofY


1 package reports;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import com.lowagie.text.*;
5 import com.lowagie.text.pdf.*;
6 import dinamica.*;
7
8 /**
9  * Template code for PDF report
10  * with Page "X of Y" footer style.
11  * <br><br>
12  * Creation date: 04/05/2005
13  * (c) 2005 Martin Cordova<br>
14  * This code is released under the LGPL license<br>
15  * Dinamica Framework - http://www.martincordova.com<br>
16  * @author Martin Cordova (dinamica@martincordova.com)
17  */

18 public class PageXofY extends AbstractPDFOutput
19 {
20
21     PdfTemplate tpl = null;
22     BaseFont bf = null;
23     PdfContentByte cb = null;
24     
25     protected void createPDF(GenericTransaction data, ByteArrayOutputStream JavaDoc buf)
26             throws Throwable JavaDoc
27     {
28
29         // initialize document - pagesize, margins, etc.
30
Document doc = new Document(PageSize.LETTER, 57,57,57,57);
31         PdfWriter docWriter = PdfWriter.getInstance(doc, buf);
32         doc.open();
33
34         // set event handler and initialize module variables
35
docWriter.setPageEvent(new PageEvents());
36         bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
37         cb = docWriter.getDirectContent();
38         tpl = cb.createTemplate(20, 14);
39         
40         doc.add(new Paragraph("This is page 1"));
41         doc.newPage();
42         
43         doc.add(new Paragraph("This is page 2"));
44         doc.newPage();
45
46         doc.add(new Paragraph("This is page 3"));
47         doc.newPage();
48
49         doc.add(new Paragraph("This is page 4"));
50         doc.newPage();
51         
52         doc.close();
53         
54     }
55
56     class PageEvents extends PdfPageEventHelper
57     {
58
59         /**
60          * Print value on empty template (total number of pages), this
61          * will update footer on every page where the template was inserted
62          */

63         public void onCloseDocument(PdfWriter writer, Document document)
64         {
65                 // print total number of pages into template
66
// this will update footer on every page
67
int pageNum = writer.getPageNumber() - 1;
68                 tpl.beginText();
69                 tpl.setFontAndSize(bf, 10);
70                 tpl.showText(String.valueOf(pageNum));
71                 tpl.endText();
72
73         }
74         
75         /**
76          * Create custom-made footer on every page, this
77          * will place the empty template on every page
78          */

79         public void onEndPage(PdfWriter writer, Document document)
80         {
81
82             // print partial footer (x of ...) on every page
83
// append template at the end of the footer, like:
84
// 1 of [template] - template is an empty box
85
String JavaDoc footer = writer.getPageNumber() + " of ";
86             float tWidth = bf.getWidthPoint(footer, 10);
87             float extraSpace = bf.getWidthPoint("00", 10);
88             float ty = document.bottom() - 14; //below bottom margin
89
float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer
90

91             // print "PageNumber of "
92
cb.beginText();
93             cb.setFontAndSize(bf, 10);
94             cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0);
95             cb.endText();
96
97             // now append empty template after "PageNumber of "
98
cb.addTemplate(tpl, document.right() - extraSpace, ty);
99             
100         }
101         
102     }
103     
104 }
105
Popular Tags