Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 views
in Technique[技术] by (71.8m points)

java - How to add multiple headers and footers in pdf using itext

In my pdf I need to have more than one header and footer.In header I want title heading on left and some text on the center.

Likewise in footer I need to print my company name on the left side,page number on center and some info regarding the contents in my table on the right side.

I have seen so many posts but I didn't get the correct idea for creating this.Somebody please help me with some example code snippets.Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Headers and footers should be added using 'page events'. If you need some examples, just look for the keyword header / footer on the official web site.

Just create a class that extends PdfPageEventHelper and implement the onEndPage() method. People who read the documentation do not make the common mistake to use the onStartPage() method, but maybe you overlooked this, so I'm adding this as an extra caveat.

Add an instance of your class to the PdfWriter object with the setPageEvent() method.

I don't know if I understand what you mean by "multiple" headers. If you have more than one page event implementation, you can add them all using the setPageEvent() method and they will all be executed. If you want to switch from one page event implementation to another, you need to use setPageEvent(null) first.

Maybe you want the header to be different for different pages, just use a member-variable in your page event implementation and change it along the way. In one of the book examples named MovieHistory2, the text for the header is stored in a String array named header.

The position of the header depends on the page number:

    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, header[0],
                    rect.getRight(), rect.getTop(), 0);
            break;
        case 1:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_LEFT, header[1],
                    rect.getLeft(), rect.getTop(), 0);
            break;
        }
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
                (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
    }

For even page numbers, the header is added to the right; for odd page numbers to the left. The footer is centered as you can see.

You also mention a header table. If you want to use a table, please take a look at the MovieCountries1 example.

You say: "I have seen so many posts but I didn't get the correct idea for creating this." You will get the correct idea by reading the documentation, more specifically chapter 5 of the book "iText in Action — Second Edition" from which the code snippets I'm referring to are taken.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...