KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > countries > web > CountriesPdfView


1 package org.springframework.samples.countries.web;
2
3 import java.awt.*;
4 import java.io.IOException JavaDoc;
5 import java.text.DateFormat JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11
12 import com.lowagie.text.Document;
13 import com.lowagie.text.DocumentException;
14 import com.lowagie.text.Element;
15 import com.lowagie.text.Font;
16 import com.lowagie.text.Paragraph;
17 import com.lowagie.text.Phrase;
18 import com.lowagie.text.pdf.BaseFont;
19 import com.lowagie.text.pdf.PdfContentByte;
20 import com.lowagie.text.pdf.PdfPCell;
21 import com.lowagie.text.pdf.PdfPTable;
22 import com.lowagie.text.pdf.PdfPageEventHelper;
23 import com.lowagie.text.pdf.PdfTemplate;
24 import com.lowagie.text.pdf.PdfWriter;
25
26 import org.springframework.beans.support.RefreshablePagedListHolder;
27 import org.springframework.beans.support.SortDefinition;
28 import org.springframework.context.NoSuchMessageException;
29 import org.springframework.context.support.MessageSourceAccessor;
30 import org.springframework.samples.countries.Country;
31 import org.springframework.web.servlet.view.document.AbstractPdfView;
32
33 /**
34  * This view demonstrates how to send a PDF file with the Spring Framework
35  * using the iText PDF library.
36  *
37  * @author Jean-Pierre Pawlak
38  * @author Juergen Hoeller
39  */

40 public class CountriesPdfView extends AbstractPdfView {
41
42     private static final Font HEADLINE_FONT = new Font( Font.HELVETICA, 18, Font.BOLD, Color.blue );
43     private static final Font HEADING_FONT = new Font( Font.HELVETICA, 12, Font.ITALIC, Color.black );
44     private static final Font HEADING_DATA_FONT = new Font( Font.HELVETICA, 12, Font.ITALIC, Color.blue );
45     private static final Font DATA_HEAD_FONT = new Font( Font.HELVETICA, 10, Font.ITALIC, Color.black );
46     private static final Font TEXT_FONT = new Font( Font.TIMES_ROMAN, 8, Font.NORMAL, Color.black );
47     private static final Font BOLD_FONT = new Font( Font.TIMES_ROMAN, 8, Font.BOLD, Color.black );
48     private static final int MARGIN = 32;
49
50     protected void buildPdfMetadata(Map JavaDoc model, Document document, HttpServletRequest JavaDoc request) {
51         document.addTitle("Countries List");
52         document.addCreator("Spring Countries");
53     }
54
55     protected void buildPdfDocument(Map JavaDoc model, Document document, PdfWriter writer,
56             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
57             throws DocumentException, NoSuchMessageException {
58
59         // We search the data to insert.
60
RefreshablePagedListHolder pgHolder = (RefreshablePagedListHolder) model.get("countries");
61         DateFormat JavaDoc df = DateFormat.getDateInstance(DateFormat.SHORT, pgHolder.getLocale());
62
63         // We prepare some data.
64
SortDefinition sort = pgHolder.getSort();
65         CountriesFilter filter = (CountriesFilter) pgHolder.getFilter();
66
67         // We create and add the event handler.
68
// So we can well paging, ensuring that only entire cells are printed
69
// at end of pages (the latter is useless in this example as records
70
// keep in one row, but in your own developpment it's not always the case).
71
MyPageEvents events = new MyPageEvents(getMessageSourceAccessor());
72         writer.setPageEvent(events);
73         events.onOpenDocument(writer, document);
74         
75         String JavaDoc title = getMessageSourceAccessor().getMessage("app.name");
76         document.add(new Paragraph(title, HEADLINE_FONT));
77         document.add(new Paragraph(" "));
78         document.add(new Paragraph(" "));
79         document.add(new Paragraph(" "));
80     
81         // We create a table for used criteria and extracting information
82
PdfPTable table = new PdfPTable(2);
83         table.setWidthPercentage(50);
84         table.getDefaultCell().setBorderWidth(1);
85         table.getDefaultCell().setBorderColor(Color.black);
86         table.getDefaultCell().setPadding(4);
87         table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
88         table.getDefaultCell().setVerticalAlignment( Element.ALIGN_MIDDLE);
89
90         PdfPCell cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage("criteria"), HEADING_FONT));
91         cell.setColspan(2);
92         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
93         cell.setGrayFill(0.7f);
94         table.addCell(cell);
95         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage("property"), HEADING_FONT));
96         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
97         cell.setGrayFill(0.9f);
98         table.addCell(cell);
99         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "value"), HEADING_FONT));
100         cell.setHorizontalAlignment(Element.ALIGN_CENTER);
101         cell.setGrayFill(0.9f);
102         table.addCell(cell);
103
104         // We put the used criteria and extracting information
105
cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "date.extraction"), HEADING_FONT));
106         table.addCell(cell);
107         cell = new PdfPCell(new Phrase(df.format(pgHolder.getRefreshDate()), HEADING_DATA_FONT));
108         table.addCell(cell);
109
110         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "nbRecords"), HEADING_FONT));
111         table.addCell(cell);
112         cell = new PdfPCell(new Phrase(String.valueOf(pgHolder.getNrOfElements()), HEADING_DATA_FONT));
113         table.addCell(cell);
114
115         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "sort.name"), HEADING_FONT));
116         table.addCell(cell);
117         cell = new PdfPCell(new Phrase(sort.getProperty(), HEADING_DATA_FONT));
118         table.addCell(cell);
119
120         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "sort.asc"), HEADING_FONT));
121         table.addCell(cell);
122         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage(new Boolean JavaDoc(sort.isAscending()).toString()),
123                 HEADING_DATA_FONT));
124         table.addCell(cell);
125
126         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "sort.igncase"), HEADING_FONT));
127         table.addCell(cell);
128         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage(new Boolean JavaDoc(sort.isIgnoreCase()).toString()),
129                 HEADING_DATA_FONT));
130         table.addCell(cell);
131
132         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "filter.name"), HEADING_FONT));
133         table.addCell(cell);
134         cell = new PdfPCell(new Phrase(null == filter.getName() ? "" : filter.getName(), HEADING_DATA_FONT));
135         table.addCell(cell);
136
137         cell = new PdfPCell(new Phrase(getMessageSourceAccessor().getMessage( "filter.code"), HEADING_FONT));
138         table.addCell(cell);
139         cell = new PdfPCell(new Phrase(null == filter.getCode() ? "" : filter.getCode(), HEADING_DATA_FONT));
140         table.addCell(cell);
141
142         document.add(table);
143         document.newPage();
144
145         // We can go now on the countries list
146
table = new PdfPTable(2);
147         int headerwidths[] = {20, 80};
148         table.setWidths(headerwidths);
149         table.setWidthPercentage(60);
150         table.getDefaultCell().setBorderWidth(2);
151         table.getDefaultCell().setBorderColor(Color.black);
152         table.getDefaultCell().setGrayFill(0.75f);
153         table.getDefaultCell().setPadding(3);
154         table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
155         table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
156
157         table.addCell(new Phrase(getMessageSourceAccessor().getMessage( "code"), DATA_HEAD_FONT));
158         table.addCell(new Phrase(getMessageSourceAccessor().getMessage( "name"), DATA_HEAD_FONT));
159
160         // We set the above row as remaining title
161
// and adjust properties for normal cells
162
table.setHeaderRows(1);
163         table.getDefaultCell().setBorderWidth(1);
164         table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
165         table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);
166     
167         // We iterate now on the countries list
168
boolean even = false;
169         Iterator JavaDoc it = pgHolder.getSource().iterator();
170         while(it.hasNext()) {
171             if (even) {
172                 table.getDefaultCell().setGrayFill(0.95f);
173                 even = false;
174             } else {
175                 table.getDefaultCell().setGrayFill(1.00f);
176                 even = true;
177             }
178             Country country = (Country)it.next();
179             table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
180             table.addCell(new Phrase(country.getCode(), BOLD_FONT));
181             table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
182             table.addCell(new Phrase(country.getName(), TEXT_FONT));
183         }
184         document.add(table);
185     }
186
187
188     //~ Inner Classes ----------------------------------------------------------
189

190     private static class MyPageEvents extends PdfPageEventHelper {
191
192         private MessageSourceAccessor messageSourceAccessor;
193
194         // This is the PdfContentByte object of the writer
195
private PdfContentByte cb;
196
197         // We will put the final number of pages in a template
198
private PdfTemplate template;
199
200         // This is the BaseFont we are going to use for the header / footer
201
private BaseFont bf = null;
202         
203         public MyPageEvents(MessageSourceAccessor messageSourceAccessor) {
204             this.messageSourceAccessor = messageSourceAccessor;
205         }
206
207         // we override the onOpenDocument method
208
public void onOpenDocument(PdfWriter writer, Document document) {
209             try {
210                 bf = BaseFont.createFont( BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED );
211                 cb = writer.getDirectContent();
212                 template = cb.createTemplate(50, 50);
213             } catch (DocumentException de) {
214             } catch (IOException JavaDoc ioe) {}
215         }
216
217         // we override the onEndPage method
218
public void onEndPage(PdfWriter writer, Document document) {
219             int pageN = writer.getPageNumber();
220             String JavaDoc text = messageSourceAccessor.getMessage("page", "page") + " " + pageN + " " +
221                 messageSourceAccessor.getMessage("on", "on") + " ";
222             float len = bf.getWidthPoint( text, 8 );
223             cb.beginText();
224             cb.setFontAndSize(bf, 8);
225
226             cb.setTextMatrix(MARGIN, 16);
227             cb.showText(text);
228             cb.endText();
229
230             cb.addTemplate(template, MARGIN + len, 16);
231             cb.beginText();
232             cb.setFontAndSize(bf, 8);
233
234             cb.endText();
235         }
236
237         // we override the onCloseDocument method
238
public void onCloseDocument(PdfWriter writer, Document document) {
239             template.beginText();
240             template.setFontAndSize(bf, 8);
241             template.showText(String.valueOf( writer.getPageNumber() - 1 ));
242             template.endText();
243         }
244     }
245
246 }
247
Popular Tags