1 package org.springframework.samples.countries.web; 2 3 import java.awt.*; 4 import java.io.IOException ; 5 import java.text.DateFormat ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 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 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 model, Document document, HttpServletRequest request) { 51 document.addTitle("Countries List"); 52 document.addCreator("Spring Countries"); 53 } 54 55 protected void buildPdfDocument(Map model, Document document, PdfWriter writer, 56 HttpServletRequest request, HttpServletResponse response) 57 throws DocumentException, NoSuchMessageException { 58 59 RefreshablePagedListHolder pgHolder = (RefreshablePagedListHolder) model.get("countries"); 61 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, pgHolder.getLocale()); 62 63 SortDefinition sort = pgHolder.getSort(); 65 CountriesFilter filter = (CountriesFilter) pgHolder.getFilter(); 66 67 MyPageEvents events = new MyPageEvents(getMessageSourceAccessor()); 72 writer.setPageEvent(events); 73 events.onOpenDocument(writer, document); 74 75 String 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 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 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 (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 (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 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 table.setHeaderRows(1); 163 table.getDefaultCell().setBorderWidth(1); 164 table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 165 table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); 166 167 boolean even = false; 169 Iterator 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 190 private static class MyPageEvents extends PdfPageEventHelper { 191 192 private MessageSourceAccessor messageSourceAccessor; 193 194 private PdfContentByte cb; 196 197 private PdfTemplate template; 199 200 private BaseFont bf = null; 202 203 public MyPageEvents(MessageSourceAccessor messageSourceAccessor) { 204 this.messageSourceAccessor = messageSourceAccessor; 205 } 206 207 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 ioe) {} 215 } 216 217 public void onEndPage(PdfWriter writer, Document document) { 219 int pageN = writer.getPageNumber(); 220 String 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 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 |