Add Classic Text Headers & Footers

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
Add Classic Text Headers & Footers

Add Classic Text Headers & Footers

This code example shows how IronPDF allows developers to define headers and footers for PDF documents loaded from a file system or generated from HTML content.

The TextHeaderFooter class enables developers to specify the text that should be included in the left, the right, or the central region of a PDF document's header or footer. As shown above, developers can quickly put together headers and footers using IronPDF's built-in templating tags (such as `{date}` `, ` `{time}` `, and ` `{page}`). Users do not need to use these tags exclusively; IronPDF gives users the freedom to truly incorporate any kind and amount of text at these locations.

Consider using the HtmlHeaderFooter class in place of the TextHeaderFooter class for far greater design flexibility within IronPDF to enhance your document presentation.

Explore the TextHeaderFooter Class Documentation
View on GitHub

Ready to Get Started?

Version:2026.10just released