KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > reports > PDFEnhancedMasterDetail


1 package reports;
2
3 import com.lowagie.text.*;
4 import com.lowagie.text.pdf.*;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import dinamica.*;
7
8 /**
9  * Customer Profile Report
10  * <br><br>
11  * (c) 2004 Martin Cordova<br>
12  * This code is released under the LGPL license<br>
13  * Dinamica Framework - http://www.martincordova.com
14  * @author Martin Cordova (dinamica@martincordova.com)
15  * */

16 public class PDFEnhancedMasterDetail extends AbstractPDFOutput
17 {
18
19     PdfTemplate tpl = null;
20     BaseFont bf = null;
21     PdfContentByte cb = null;
22     
23     /* (non-Javadoc)
24      * @see dinamica.AbstractPDFOutput#createPDF(dinamica.GenericTransaction, java.io.ByteArrayOutputStream)
25      */

26     protected void createPDF(
27         GenericTransaction data,
28         ByteArrayOutputStream JavaDoc buf)
29         throws Throwable JavaDoc
30     {
31
32         //init pdf - pagesize, margins, etc.
33
Document doc = new Document();
34         PdfWriter docWriter = PdfWriter.getInstance(doc, buf);
35         doc.setPageSize(PageSize.LETTER);
36         
37         //header
38
HeaderFooter header = new HeaderFooter(new Phrase(getHeader()), false);
39         header.setBorder(Rectangle.BOTTOM);
40         header.setAlignment(Rectangle.ALIGN_CENTER);
41         doc.setHeader(header);
42
43         doc.open();
44
45         // set event handler and module variables
46
docWriter.setPageEvent(new PageEvents());
47         bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
48         cb = docWriter.getDirectContent();
49         tpl = cb.createTemplate(20, 14);
50         
51         //activate outline
52
docWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines);
53         PdfOutline root = cb.getRootOutline();
54     
55         //title
56
root = new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Customer Data");
57         Paragraph t = new Paragraph(getReportTitle(), new Font(Font.HELVETICA, 18f));
58         t.setAlignment(Rectangle.ALIGN_CENTER);
59         doc.add(t);
60
61         //customer basic info
62
PdfPTable custinfo = getCustomerSection(data);
63         doc.add(custinfo);
64
65         //for each master record print a master/detail section
66
MasterDetailReader dataobj = (MasterDetailReader)data;
67         Recordset master = dataobj.getRecordset("master");
68         master.top();
69         while (master.next())
70         {
71             //blank line
72
doc.add(new Paragraph(" "));
73
74             PdfPTable tbl = getGroupDetail(master, dataobj.getDetail(master));
75             
76             if (!docWriter.fitsPage(tbl, 25))
77                 doc.newPage();
78             
79             //print master section
80
new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Order #" + master.getString("orderid"));
81             doc.add( getGroupMaster(master) );
82             
83             //print detail section with subtotal
84
doc.add( tbl );
85             
86         }
87         
88         doc.add(new Paragraph(" "));
89
90         //print grand total
91
new PdfOutline(root, new PdfDestination(PdfDestination.XYZ, 0, docWriter.getVerticalPosition(false), 0), "Grand Total");
92         doc.add( getTotal(dataobj) );
93         
94         doc.close();
95         docWriter.close();
96         
97     }
98
99     /**
100      * Return a report section formatted as a table
101      * @param data
102      * @return
103      */

104     PdfPTable getCustomerSection(GenericTransaction data) throws Throwable JavaDoc
105     {
106
107         /*
108         * customer basic info
109         */

110         
111         //get recordset
112
Recordset custinfo = data.getRecordset("query.sql");
113         custinfo.first();
114         
115         //cols
116
PdfPTable datatable = new PdfPTable(2);
117             
118         //header
119
datatable.getDefaultCell().setPadding(3);
120         int headerwidths[] = {30, 70}; // percentage
121
datatable.setWidths(headerwidths);
122         datatable.setWidthPercentage(50); // percentage
123
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
124         datatable.getDefaultCell().setBorderWidth(1);
125         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
126         
127         PdfPCell c1 = new PdfPCell(new Phrase("Contacto"));
128         c1.setGrayFill(0.9f);
129         datatable.addCell(c1);
130         datatable.addCell(custinfo.getString("contactname"));
131         
132         PdfPCell c2 = new PdfPCell(new Phrase("Empresa"));
133         c2.setGrayFill(0.9f);
134         datatable.addCell(c2);
135         datatable.addCell(custinfo.getString("companyname"));
136         
137         PdfPCell c3 = new PdfPCell(new Phrase("Teléfono"));
138         c3.setGrayFill(0.9f);
139         datatable.addCell(c3);
140         datatable.addCell(custinfo.getString("phone"));
141         
142         PdfPCell c4 = new PdfPCell(new Phrase("País"));
143         c4.setGrayFill(0.9f);
144         datatable.addCell(c4);
145         datatable.addCell(custinfo.getString("country"));
146
147         datatable.setSpacingBefore(10);
148         return datatable;
149         
150     }
151
152     /**
153      * Return group header (master)
154      * @param master Recordset containing master records
155      * @return
156      * @throws Throwable
157      */

158     Paragraph getGroupMaster(Recordset master) throws Throwable JavaDoc
159     {
160
161         String JavaDoc text = "Order #" + master.getString("orderid");
162         text = text + " Date: " + StringUtil.formatDate(master.getDate("orderdate"), "dd-MMM-yyyy");
163         
164         Paragraph t = new Paragraph(text,new Font(Font.HELVETICA, 12f, Font.BOLD));
165         t.setAlignment(Rectangle.ALIGN_LEFT);
166         return t;
167         
168     }
169
170     /**
171      * Return a report section formatted as a table
172      * @param data
173      * @return
174      */

175     PdfPTable getGroupDetail(Recordset master, Recordset detail) throws Throwable JavaDoc
176     {
177
178         //cols
179
PdfPTable datatable = new PdfPTable(6);
180             
181         //header
182
datatable.getDefaultCell().setPadding(1);
183         int headerwidths[] = {35, 20, 10, 10, 10, 15}; // percentage
184
datatable.setWidths(headerwidths);
185         datatable.setWidthPercentage(100); // percentage
186
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
187         datatable.getDefaultCell().setBorderWidth(1);
188         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
189         datatable.getDefaultCell().setGrayFill(0.9f);
190         datatable.addCell("Product");
191         datatable.addCell("Category");
192         datatable.addCell("Quantity");
193         datatable.addCell("Unit Price");
194         datatable.addCell("Discount");
195         datatable.addCell("Sub-Total");
196
197         datatable.setHeaderRows(1);
198         datatable.getDefaultCell().setGrayFill(0.0f);
199         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
200         
201         //orders
202
while (detail.next())
203         {
204             String JavaDoc data = null;
205             
206             datatable.addCell(detail.getString("productname"));
207             datatable.addCell(detail.getString("categoryname"));
208         
209             PdfPCell c1 = new PdfPCell(new Phrase(detail.getString("quantity")));
210             c1.setHorizontalAlignment(Element.ALIGN_RIGHT);
211             datatable.addCell(c1);
212
213             data = StringUtil.formatNumber(detail.getValue("unitprice"),"#,##0.00");
214             PdfPCell c2 = new PdfPCell(new Phrase(data));
215             c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
216             datatable.addCell(c2);
217
218             data = StringUtil.formatNumber(detail.getValue("discount"),"#,##0.00");
219             PdfPCell c3 = new PdfPCell(new Phrase(data));
220             c3.setHorizontalAlignment(Element.ALIGN_RIGHT);
221             datatable.addCell(c3);
222
223             data = StringUtil.formatNumber(detail.getValue("subtotal"),"#,##0.00");
224             PdfPCell c4 = new PdfPCell(new Phrase(data));
225             c4.setHorizontalAlignment(Element.ALIGN_RIGHT);
226             datatable.addCell(c4);
227
228         }
229
230         //total
231
PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD)));
232         c1.setColspan(5);
233         datatable.addCell(c1);
234
235         PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(master.getValue("total"), "#,##0.00")));
236         c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
237         datatable.addCell(c2);
238
239         datatable.setSpacingBefore(10);
240         return datatable;
241         
242     }
243
244     /**
245      * Return a report section formatted as a table
246      * @param data
247      * @return
248      */

249     PdfPTable getTotal(MasterDetailReader dataobj) throws Throwable JavaDoc
250     {
251
252         //cols
253
PdfPTable datatable = new PdfPTable(2);
254             
255         //header
256
datatable.getDefaultCell().setPadding(1);
257         int headerwidths[] = {85, 15}; // percentage
258
datatable.setWidths(headerwidths);
259         datatable.setWidthPercentage(100); // percentage
260
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
261         datatable.getDefaultCell().setBorderWidth(1);
262         datatable.getDefaultCell().setGrayFill(0.0f);
263
264         //total
265
Recordset custinfo = dataobj.getRecordset("query.sql");
266         custinfo.first();
267         
268         PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD)));
269         datatable.addCell(c1);
270
271         PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(custinfo.getValue("total"), "#,##0.00")));
272         c2.setHorizontalAlignment(Element.ALIGN_RIGHT);
273         datatable.addCell(c2);
274
275         return datatable;
276         
277     }
278
279     /**
280      * Inner class used to intercept page events
281      * and create the Page X of Y effect
282      *
283      */

284     class PageEvents extends PdfPageEventHelper
285     {
286
287         public void onCloseDocument(PdfWriter writer, Document document)
288         {
289                 // print total number of pages into template
290
// this will update footer on every page
291
int pageNum = writer.getPageNumber() - 1;
292                 tpl.beginText();
293                 tpl.setFontAndSize(bf, 10);
294                 tpl.showText(String.valueOf(pageNum));
295                 tpl.endText();
296
297         }
298         
299         public void onEndPage(PdfWriter writer, Document document)
300         {
301
302             // print partial footer (x of ...) on every page
303
// append template at the end of the footer, like:
304
// 1 of [template] - template is an empty box
305
String JavaDoc footer = writer.getPageNumber() + " of ";
306             float tWidth = bf.getWidthPoint(footer, 10);
307             float extraSpace = bf.getWidthPoint("00", 10);
308             float ty = document.bottom() - 14; //below bottom margin
309
float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer
310
cb.beginText();
311             cb.setFontAndSize(bf, 10);
312             cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0);
313             cb.endText();
314             cb.addTemplate(tpl, document.right() - extraSpace, ty);
315             
316         }
317         
318     }
319     
320 }
321
Popular Tags