Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - How to add text as a header or footer?

I'm creating a pdf with iText 5 and want to add a footer. I did everything like the book "iText in action" in Chapter 14 says.

There are no errors but the footer doesn't show up. Can somebody tell me what I'm doing wrong?

My code:

public class PdfBuilder {

    private Document document;

    public void newDocument(String file) {
        document = new Document(PageSize.A4);
        writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        MyFooter footerEvent = new MyFooter();
        writer.setPageEvent(footerEvent);
        document.open();

        ...

        document.close();
        writer.flush();
        writer.close();
    }

    class MyFooter extends PdfPageEventHelper {

    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer(), (document.right() - document.left()) / 2
                + document.leftMargin(), document.top() + 10, 0);

    }

    private Phrase footer() {
        Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);
        Phrase p = new Phrase("this is a footer");
        return p;
    }
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The problem you report can not be reproduced. I have taken your example and I create the TextFooter example with this event:

class MyFooter extends PdfPageEventHelper {
    Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);

    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte cb = writer.getDirectContent();
        Phrase header = new Phrase("this is a header", ffont);
        Phrase footer = new Phrase("this is a footer", ffont);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
                header,
                (document.right() - document.left()) / 2 + document.leftMargin(),
                document.top() + 10, 0);
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER,
                footer,
                (document.right() - document.left()) / 2 + document.leftMargin(),
                document.bottom() - 10, 0);
    }
}

Note that I improved the performance by creating the Font and Paragraph instance only once. I also introduced a footer and a header. You claimed you wanted to add a footer, but in reality you added a header.

The top() method gives you the top of the page, so maybe you meant to calculate the y position relative to the bottom() of the page.

There was also an error in your footer() method:

private Phrase footer() {
    Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);
    Phrase p = new Phrase("this is a footer");
    return p;
}

You define a Font named ffont, but you don't use it. I think you meant to write:

private Phrase footer() {
    Font ffont = new Font(Font.FontFamily.UNDEFINED, 5, Font.ITALIC);
    Phrase p = new Phrase("this is a footer", ffont);
    return p;
}

Now when we look at the resulting PDF, we clearly see the text that was added as a header and a footer to each page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...