repeat header and sub header on every page in itext

Hi,

I am using header and subheader methods which will print the Header and subheade information on PDF.

Header and subheader info is printed correctly on the first page and only subheader is printed from page 2 to last page.(I guess subHeader is overriding the header info from 2nd page)

How can  I repeat this header and subheader on every page.

Please advice!

Thanks!

main method{						
document.open();
addHeader(document);
addSubheader(document);
}

private static void addHeader(Document document) throws Exception {
		Paragraph paragraph;
		SimpleDateFormat format;
		Font cellFont;
		cellFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL);
		Date date = new Date();
		Table header;
		Cell cell;
		Image img;
		header = new Table(3);
		header.setBorderWidth(0);
		header.setWidth(100);
		img = Image
				.getInstance("/abcd.jpg");
		cell = new Cell(img);
		cell.setBorderWidth(0);
		cell.disableBorderSide(Cell.LEFT);
		cell.disableBorderSide(Cell.TOP);
		cell.disableBorderSide(Cell.RIGHT);
		cell.disableBorderSide(Cell.BOTTOM);
		header.addCell(cell);
		paragraph = new Paragraph(new Chunk("System",
				FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD)));
		paragraph.add(new Chunk("\nInput", FontFactory
				.getFont(FontFactory.HELVETICA, 12, Font.NORMAL)));
		cell = new Cell(paragraph);
		cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
		cell.disableBorderSide(Cell.LEFT);
		cell.disableBorderSide(Cell.TOP);
		cell.disableBorderSide(Cell.RIGHT);
		cell.disableBorderSide(Cell.BOTTOM);
		header.addCell(cell);

		format = new SimpleDateFormat("MM/dd/yyyy \nhh:mm:ss aaa");
		cell = new Cell(new Chunk(format.format(new Date())));
		cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
		cell.disableBorderSide(Cell.LEFT);
		cell.disableBorderSide(Cell.TOP);
		cell.disableBorderSide(Cell.RIGHT);
		cell.disableBorderSide(Cell.BOTTOM);
		header.addCell(cell);
		
		Phrase headerPhrase = new Phrase();
		headerPhrase.add(header);
		HeaderFooter printHeader = new HeaderFooter(headerPhrase,false);
		System.out.println("addHeader");
		document.setHeader(printHeader);
		header.setLastHeaderRow(1);
		
		header.endHeaders();
		document.add(header);
}
	private static void addSubheader(Document document) throws Exception {

		Table subHeader = new Table(1);
		Cell cell;
		subHeader.setPadding(2);
		subHeader.setWidth(100);
		subHeader.setBorder(0);

		cell = new Cell(new Chunk("Sub Header", FontFactory
				.getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
		cell.setBorderWidth(0);
		cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
		cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
		cell.disableBorderSide(Cell.LEFT);
		cell.disableBorderSide(Cell.TOP);
		cell.disableBorderSide(Cell.RIGHT);
		cell.disableBorderSide(Cell.BOTTOM);
		subHeader.setBottom(2);
		subHeader.addCell(cell);
		
		Phrase subHeaderPhrase = new Phrase();
		subHeaderPhrase.add(subHeader);
		HeaderFooter printSubHeader = new HeaderFooter(subHeaderPhrase,false);
		System.out.println("addSubheader");
		document.setHeader(printSubHeader);
		subHeader.setLastHeaderRow(1);
		
		subHeader.endHeaders();
		document.add(subHeader);

	}

Open in new window

thearthaAsked:
Who is Participating?
 
jb1devConnect With a Mentor Commented:
The iText API doesnt' seem to have the concept of a "sub header".
So when you call setHeader() the second time, I'm guessing you are replacing the existing header with the second header.

Can you just add the "subheader" to the Phrase used in the first header?

Try this example:

import java.io.*;
import java.util.*;
import java.text.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Foo {

    public static void main(String[] args) throws Exception {

        Document document = new Document();
        PdfWriter pdf = PdfWriter.getInstance(document,
                            new FileOutputStream("foo.pdf"));
        
        addHeader(document);

        document.open();

        // Sample data...
        Chapter chapter1 =
            new Chapter(new Paragraph("This is the first Chapter"), 1);
        Chapter chapter2 =
            new Chapter(new Paragraph("This is second chapter"), 2);
        document.add(chapter1);
        document.add(chapter2);

        document.close();
    }

    private static void addHeader(Document document) throws Exception {
        Paragraph paragraph;
        SimpleDateFormat format;
        Font cellFont;
        cellFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL);
        Date date = new Date();
        Table header;
        Cell cell;
        Image img;
        header = new Table(3);
        header.setBorderWidth(0);
        header.setWidth(100);
        img = Image
                .getInstance("./abcd.jpg");
        cell = new Cell(img);
        cell.setBorderWidth(0);
        cell.disableBorderSide(Cell.LEFT);
        cell.disableBorderSide(Cell.TOP);
        cell.disableBorderSide(Cell.RIGHT);
        cell.disableBorderSide(Cell.BOTTOM);
        header.addCell(cell);
        paragraph = new Paragraph(new Chunk("System",
                FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD)));
        paragraph.add(new Chunk("\nInput", FontFactory
                .getFont(FontFactory.HELVETICA, 12, Font.NORMAL)));
        cell = new Cell(paragraph);
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
        cell.disableBorderSide(Cell.LEFT);
        cell.disableBorderSide(Cell.TOP);
        cell.disableBorderSide(Cell.RIGHT);
        cell.disableBorderSide(Cell.BOTTOM);
        header.addCell(cell);

        format = new SimpleDateFormat("MM/dd/yyyy \nhh:mm:ss aaa");
        cell = new Cell(new Chunk(format.format(new Date())));
        cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
        cell.disableBorderSide(Cell.LEFT);
        cell.disableBorderSide(Cell.TOP);
        cell.disableBorderSide(Cell.RIGHT);
        cell.disableBorderSide(Cell.BOTTOM);
        header.addCell(cell);
        
        Phrase headerPhrase = new Phrase();
        headerPhrase.add(header);
        HeaderFooter printHeader = new HeaderFooter(headerPhrase,false);

        Phrase subPhrase = getSubheader();
        headerPhrase.add(subPhrase);

        System.out.println("addHeader");

        document.setHeader(printHeader);
        header.setLastHeaderRow(1);
        header.endHeaders();

    }

    private static Phrase getSubheader() throws Exception {

        Table subHeader = new Table(1);
        Cell cell;
        subHeader.setPadding(2);
        subHeader.setWidth(100);
        subHeader.setBorder(0);

        cell = new Cell(new Chunk("Sub Header", FontFactory
                .getFont(FontFactory.HELVETICA, 14, Font.BOLD)));
        cell.setBorderWidth(0);
        cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
        cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
        cell.disableBorderSide(Cell.LEFT);
        cell.disableBorderSide(Cell.TOP);
        cell.disableBorderSide(Cell.RIGHT);
        cell.disableBorderSide(Cell.BOTTOM);
        subHeader.setBottom(2);
        subHeader.addCell(cell);
        
        Phrase subHeaderPhrase = new Phrase();
        subHeaderPhrase.add(subHeader);
        subHeader.setLastHeaderRow(1);
        subHeader.endHeaders();

        return subHeaderPhrase;
    }
}

Open in new window

0
 
jb1devCommented:
Do you need to add headers before calling document.open()?

Check this thread:
http://www.experts-exchange.com/Programming/Languages/Java/Q_24981220.html


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.