View Javadoc

1   /**
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
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  /**
28   * PDF exporter using iText.
29   * @author Jorge L. Barroso
30   * @version $Revision$ ($Author$)
31   */
32  public class DefaultPdfExportView extends DefaultItextExportView
33  {
34  
35      /**
36       * @see org.displaytag.export.BaseExportView#getMimeType()
37       * @return "application/pdf"
38       */
39      public String getMimeType()
40      {
41          return "application/pdf"; //$NON-NLS-1$
42      }
43  
44      /**
45       * Initializes the PDF writer this export view uses to write the table document.
46       * @param document The iText document to be written.
47       * @param out The output stream to which the document is written.
48       * @throws DocumentException If something goes wrong during initialization.
49       */
50      protected void initItextWriter(Document document, OutputStream out) throws DocumentException
51      {
52          PdfWriter.getInstance(document, out).setPageEvent(new PageNumber());
53      }
54  
55      /**
56       * Prints a page number at the bottom of each page. Based on
57       * http://itextdocs.lowagie.com/examples/com/lowagie/examples/directcontent/pageevents/PageNumbersWatermark.java
58       * @author Jorge L. Barroso
59       * @version $Revision$ ($Author$)
60       */
61      private static class PageNumber extends PdfPageEventHelper
62      {
63  
64          /**
65           * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
66           * com.lowagie.text.Document)
67           */
68          public void onEndPage(PdfWriter writer, Document document)
69          {
70              /** The headertable. */
71              PdfPTable table = new PdfPTable(2);
72              /** A template that will hold the total number of pages. */
73              PdfTemplate tpl = writer.getDirectContent().createTemplate(100, 100);
74              /** The font that will be used. */
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              // write the headertable
89              table.setTotalWidth(document.right() - document.left());
90              table.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 50, cb);
91              // compose the footer
92              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 }