1 12 package org.displaytag.export; 13 14 import java.io.IOException ; 15 import java.io.OutputStream ; 16 17 import com.lowagie.text.Document; 18 import com.lowagie.text.DocumentException; 19 import com.lowagie.text.pdf.BaseFont; 20 import com.lowagie.text.pdf.PdfContentByte; 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 27 32 public class DefaultPdfExportView extends DefaultItextExportView 33 { 34 35 39 public String getMimeType() 40 { 41 return "application/pdf"; } 43 44 50 protected void initItextWriter(Document document, OutputStream out) throws DocumentException 51 { 52 PdfWriter.getInstance(document, out).setPageEvent(new PageNumber()); 53 } 54 55 61 private static class PageNumber extends PdfPageEventHelper 62 { 63 64 68 public void onEndPage(PdfWriter writer, Document document) 69 { 70 71 PdfPTable table = new PdfPTable(2); 72 73 PdfTemplate tpl = writer.getDirectContent().createTemplate(100, 100); 74 75 BaseFont helv = null; 76 try 77 { 78 helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); 79 } 80 catch (DocumentException e) 81 { 82 } 83 catch (IOException e) 84 { 85 } 86 PdfContentByte cb = writer.getDirectContent(); 87 cb.saveState(); 88 table.setTotalWidth(document.right() - document.left()); 90 table.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 50, cb); 91 String text = "Page " + writer.getPageNumber(); 93 float textSize = helv.getWidthPoint(text, 12); 94 float textBase = document.bottom() - 20; 95 cb.beginText(); 96 cb.setFontAndSize(helv, 12); 97 float adjust = helv.getWidthPoint("0", 12); 98 cb.setTextMatrix(document.right() - textSize - adjust, textBase); 99 cb.showText(text); 100 cb.endText(); 101 cb.addTemplate(tpl, document.right() - adjust, textBase); 102 cb.saveState(); 103 } 104 } 105 } 106 | Popular Tags |