0

Hi guys i have a java class which is used to show header and footer in iText PDF.

the page number is showing in top of the page and footer is coming correctly in footer section.. now i want to display the page number in footer place...

following code im using

public HeaderAndFooter() {

header = new Phrase("**** Header ****");

footer = new PdfPTable(1);

footer.setTotalWidth(130);

footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

footer.addCell(new Phrase(new Chunk(

"**** Footer summary****")

.setAction(new PdfAction(PdfAction.FIRSTPAGE))));

}

public void onEndPage(PdfWriter writer, Document document) {

PdfContentByte cb = writer.getDirectContent();

ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase(String.format("%d", writer.getPageNumber()),

PDFUtil.getNormalFont()), (document

.right() - document.left())

/ 2 + document.leftMargin(), document.top() + 10, 0);

footer.writeSelectedRows(0, -1,

(document.right() - document.left() - 300) / 2

+ document.leftMargin(), document.bottom() - 10, cb);

}


}

im getting output like this

                                        1 (header)

26-Aug-2011 Rentaldetails August ---- 5500 month

miscellaneous----500 total ---5500

                                                                                                               **** Footer summary (footer)
                            **** 

kindly suggest me how to show pagenumber in footer section...Thankxxx in Advance

Aravinth
  • 363
  • 2
  • 10
  • 33
  • What version of IText? Older versions have the HeaderFooter object – Sean Nov 04 '11 at 14:42
  • What is the output of your Phrase with writer.getPageNumber()? If it is blank, try adding some text around it to see if there may be other issues. that is the call to get the page number. you should be able to format accordingly around that – Sean Nov 07 '11 at 16:14

3 Answers3

1

Full working class from iTEXT

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

class TableHeader extends PdfPageEventHelper {
    /** The header text. */
    String header;
    /** The template with the total number of pages. */
    PdfTemplate total;

    /**
     * Allows us to change the content of the header.
     * 
     * @param header
     *            The new header String
     */
    public void setHeader(String header) {
        this.header = header;
    }

    /**
     * Creates the PdfTemplate that will hold the total number of pages.
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(30, 16);
    }

    /**
     * Adds a header to every page
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onEndPage(PdfWriter writer, Document document) {
        PdfPTable table = new PdfPTable(3);
        try {
            table.setWidths(new int[] { 24, 24, 2 });
            table.setTotalWidth(527);
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(20);
            table.getDefaultCell().setBorder(Rectangle.BOTTOM);
            table.addCell(header);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell(String.format("Page %d of", writer.getPageNumber()));
            PdfPCell cell = new PdfPCell(Image.getInstance(total));
            cell.setBorder(Rectangle.BOTTOM);
            table.addCell(cell);
            table.writeSelectedRows(0, -1, 34, 50, writer.getDirectContent());
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

    /**
     * Fills out the total number of pages before the document is closed.
     * 
     * @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter,
     *      com.itextpdf.text.Document)
     */
    public void onCloseDocument(PdfWriter writer, Document document) {
        ColumnText.showTextAligned(total, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber() - 1)), 2, 2, 0);
    }
}
Makky
  • 17,117
  • 17
  • 63
  • 86
1

Guys i have found the solution and chaged the class too.. here itzzz...

public class HeaderAndFooter extends PdfPageEventHelper {

public void onEndPage (PdfWriter writer, Document document) {
    Rectangle rect = writer.getBoxSize("art");
    switch(writer.getPageNumber() % 2) {
    case 0:
        ColumnText.showTextAligned(writer.getDirectContent(),                            Element.ALIGN_RIGHT, new Phrase("even header"),
                rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
        break;
    case 1:
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                300f, 62f, 0);
        break;
    }

below code you have to give in your print class finally the byte stream expels out... there you have to give

baos = new ByteArrayOutputStream();
        Document document = new Document(PageSize.A4, 60, 60, 120, 80);
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        HeaderAndFooter event = new HeaderAndFooter();
        writer.setPageEvent(event);
        document.open();

It works Fine and Simple for PageNumber generation.... Note: i have used iText 2.1.0.jar

Aravinth
  • 363
  • 2
  • 10
  • 33
0

It has info about adding page numbers (only in the page and not particularly in the footer ) How to add total page number on every page with iText?

Community
  • 1
  • 1