?
Solved

repeat header and sub header on every page in itext

Posted on 2009-12-18
2
Medium Priority
?
7,391 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:theartha
  • 2
2 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 26084162
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
 
LVL 14

Accepted Solution

by:
jb1dev earned 2000 total points
ID: 26084331
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question