IRONSOFTWAREHOME
添加经典文本页眉和页脚

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.font.FontTypes;
import com.ironsoftware.ironpdf.headerfooter.TextHeaderFooter;
import com.ironsoftware.ironpdf.headerfooter.HeaderFooterOptions;
import java.io.IOException;

// Initialize HeaderFooterOptions object.
HeaderFooterOptions options = new HeaderFooterOptions();
PdfDocument pdf = PdfDocument.renderUrlAsPdf("http://www.google.com");

// Add a header to every page easily
// Mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
options.setFirstPageNumber(1); // use 2 if a coverpage  will be appended
TextHeaderFooter textHeader = new TextHeaderFooter();
textHeader.setDrawDividerLine(true);
textHeader.setCenterText("{url}");
textHeader.setFont(FontTypes.getHelvetica());
textHeader.setFontSize(12);
pdf.addTextHeader(textHeader, options);

// Add a footer too
TextHeaderFooter textFooter = new TextHeaderFooter();
textFooter.setDrawDividerLine(true);
textFooter.setFont(FontTypes.getArial());
textFooter.setFontSize(10);
textFooter.setLeftText("{date} {time}");
textFooter.setRightText("{page} of {total-pages}");
pdf.addTextFooter(textFooter, options);

try {
    pdf.saveAs(Paths.get("assets/text_headers_footers.pdf"));
} catch (IOException e) {
    System.out.println("Failed to save PDF");
    throw new RuntimeException(e);
}
mvn install
添加经典文本页眉和页脚

添加经典文本页眉和页脚

此代码示例显示了IronPDF如何允许开发人员为从文件系统加载或从HTML内容生成的PDF文档定义页眉和页脚。

TextHeaderFooter类允许开发者指定应包括在PDF文档页眉或页脚的左部、右部或中部区域的文本。 如上所示,开发人员可以使用IronPDF的内置模板标签(如`{date}` `, ` `{time}` `, and ` `{page}`)快速组合页眉和页脚。 用户不需要专门使用这些标签; IronPDF赋予用户在这些位置真正融合任何类型和数量文本的自由。

考虑在IronPDF中使用TextHeaderFooter类,以便在文档展示中获得更大的设计灵活性。

Explore the TextHeaderFooter Class Documentation
在 GitHub 上查看

准备开始了吗?

版本:2026.6刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户