如何将文本添加为​​页眉或页脚? [英] How to add text as a header or footer?

查看:127
本文介绍了如何将文本添加为​​页眉或页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText 5创建一个pdf,并希望添加一个页脚。我做了第14章中的iText in action一书。

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?

我的代码:

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;
    }
}


推荐答案

您报告的问题无法复制。我已经举了你的例子,我用这个事件创建了 TextFooter 示例:

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);
    }
}

请注意,我通过创建<$来提高性能c $ c>字体和段落实例只有一次。我还介绍了一个页脚和一个标题。您声称要添加页脚,但实际上您添加了标题。

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.

top()方法给你页面的顶部,所以也许你想计算相对于 bottom() y 位置页面。

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;
}

您定义字体命名 ffont ,但你不使用它。我想你打算写:

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;
}

现在我们看一下生成的PDF ,我们清楚地看到作为页眉和页脚添加到每个页面的文本。

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.

这篇关于如何将文本添加为​​页眉或页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆