Link to home
Start Free TrialLog in
Avatar of theartha
thearthaFlag for United States of America

asked on

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

Avatar of jb1dev
jb1dev

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

Check this thread:
https://www.experts-exchange.com/questions/24981220/adding-only-footer-in-itext.html


ASKER CERTIFIED SOLUTION
Avatar of jb1dev
jb1dev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial