栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何添加文本作为页眉或页脚?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何添加文本作为页眉或页脚?

您报告的问题无法重现。我以您的示例为例,并使用此事件创建了TextFooter示例:

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

请注意,我只创建了一次

Font
Paragraph
实例,从而提高了性能。我还介绍了页脚和页眉。您声称要添加页脚,但实际上您添加了页眉。

top()
方法使您位于页面顶部,因此也许您打算计算
y
相对于页面顶部的位置
bottom()

您的

footer()
方法中还有一个错误:

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

您定义了一个

Font
named
ffont
,但是您不使用它。我想你打算写:

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时,我们清楚地看到了作为每个页面的页眉和页脚添加的文本。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498673.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号