5 Replies Latest reply on Dec 1, 2009 9:36 PM by Marcio Dantas

    seam pdf - Page X of Y

    Marcio Dantas Newbie


      i want to display in my pdf's footer generated by seam a Page X of Y text.

      The X value can be obtained by using the tag pageNumber. My problem is getting Y.

      I found this from iText:

      I found other resources

      To work, I would have to catch the event of the document closing.
      Can this be done? Another ideas?


        • 1. Re: seam pdf - Page X of Y
          Marcio Dantas Newbie

          Ignore the I found other resources!


          • 2. Re: seam pdf - Page X of Y
            Marcio Dantas Newbie


            I found a way of doing the job. Don't know if is the best, but here it comes.

            My solution is based on the above link from iText (itext: page X of Y).

            To put it working with seam, I extended de UIDocument class, wich represents a seam pdf document.
            The encodeBegin method was overriden. In its new version, I call the former encodeBegin (required), set the page event listener in the pdf writer, and initialize the template and font that will be used to hold and print, respectively, the number of total pages at the onCloseDocument event.

            To use the new document in my pages I simply added a binding attribute to my template.
            Below, you'll find the code.

            New component:

            package br.gov.esaf.sgc.view;
            import java.io.IOException;
            import javax.faces.context.FacesContext;
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            import org.jboss.seam.pdf.ui.UIDocument;
            import com.lowagie.text.DocWriter;
            import com.lowagie.text.Document;
            import com.lowagie.text.DocumentException;
            import com.lowagie.text.ExceptionConverter;
            import com.lowagie.text.Rectangle;
            import com.lowagie.text.pdf.BaseFont;
            import com.lowagie.text.pdf.PdfContentByte;
            import com.lowagie.text.pdf.PdfPageEventHelper;
            import com.lowagie.text.pdf.PdfTemplate;
            import com.lowagie.text.pdf.PdfWriter;
             * PDF Document with "Page X of Y" footer.
            public class SeamPdfDocument extends UIDocument {
                 * Template que guarda o número total de páginas.
                private PdfTemplate templateTotalPaginas;
                 * A fonte que será usada.
                 * */
                private BaseFont fonte;
                /** {@inheritDoc} */
                public void encodeBegin(FacesContext context) throws IOException {
                    // chama encode begin do componente pai
                    // customiza página usando listener
                    DocWriter writer = getWriter();
                    if (writer != null && writer instanceof PdfWriter) {
                        // recupera pdfwriter e seta listener
                        PdfWriter pdfWriter = (PdfWriter) writer;
                        pdfWriter.setPageEvent(new PdfDocumentEventListener());
                        // inicialização de template
                        templateTotalPaginas = pdfWriter.getDirectContent().createTemplate(100, 100);
                        templateTotalPaginas.setBoundingBox(new Rectangle(-20, -20, 100, 100));
                        // inicialização de fonte
                        try {
                            fonte = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false);
                        } catch (IOException e) {
                            throw new ExceptionConverter(e);
                        } catch (DocumentException e) {
                            throw new ExceptionConverter(e);
                 * Listener para eventos na criação da página PDF.
                private class PdfDocumentEventListener extends PdfPageEventHelper {
                    /** {@inheritDoc} */
                    public void onEndPage(PdfWriter writer, Document document) {
                        PdfContentByte cb = writer.getDirectContent();
                        // compose the footer
                        String text = "Page " + writer.getPageNumber() + " of ";
                        float textSize = fonte.getWidthPoint(text, 12);
                        float textBase = document.bottom() - 20;
                        cb.setFontAndSize(fonte, 12);
                        // for odd pagenumbers, show the footer at the left
                        if ((writer.getPageNumber() & 1) == 1) {
                            cb.setTextMatrix(document.left(), textBase);
                                .addTemplate(templateTotalPaginas, document.left() + textSize,
                        // for even numbers, show the footer at the right
                        } else {
                            float adjust = fonte.getWidthPoint("0", 12);
                            cb.setTextMatrix(document.right() - textSize - adjust, textBase);
                            cb.addTemplate(templateTotalPaginas, document.right() - adjust, textBase);
                    /** {@inheritDoc} */
                    public void onCloseDocument(PdfWriter writer, Document document) {
                        templateTotalPaginas.setFontAndSize(fonte, 12);
                        templateTotalPaginas.setTextMatrix(0, 0);
                        templateTotalPaginas.showText(Integer.toString(writer.getPageNumber() - 1));

            Usage on pages:


            • 3. Re: seam pdf - Page X of Y
              Robert Becker Newbie
              Hi Márcio,

              thanks for your code. I am too using it as an workarround for now.
              I'll open up a feature request for a <p:totalPages>-tag because i think this is essential especially for official documents!

              Greetings Robert
              • 5. Re: seam pdf - Page X of Y
                Marcio Dantas Newbie

                Thanks for the JIRA Robert. Let's see if something better than above appears.