Add PDF Headers and Footers in Java
Contents
[
Hide
]
Aspose.PDF for Java lets you assign HeaderFooter objects to each page and populate them with different content types.
Add text headers and footers
Use this example when you need simple text content at the top and bottom of each page.
- Create HeaderFooter objects and add text fragments.
- Configure margins for the header and footer.
- Apply them to each page of the source PDF and save the result.
public static void addHeaderAndFooterAsText(Path inputFile, Path outputFile) {
HeaderFooter header = new HeaderFooter();
header.getParagraphs().add(new TextFragment("Demo header"));
HeaderFooter footer = new HeaderFooter();
footer.getParagraphs().add(new TextFragment("Demo footer"));
MarginInfo margin = new MarginInfo();
margin.setLeft(50);
margin.setTop(20);
header.setMargin(margin);
footer.setMargin(margin);
try (Document document = new Document(inputFile.toString())) {
for (int i = 1; i <= document.getPages().size(); i++) {
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}
Add headers and footers with page numbering
Use this example when the header or footer should show the current page number and total page count.
- Create HeaderFooter objects with page numbering placeholders.
- Configure margins for both objects.
- Apply them to each page and save the updated PDF.
public static void usingHeaderAndFooterForPageNumbering(Path inputFile, Path outputFile) {
HeaderFooter header = new HeaderFooter();
header.getParagraphs().add(new TextFragment("Page $p from $P"));
HeaderFooter footer = new HeaderFooter();
footer.getParagraphs().add(new TextFragment("Page $p / $P"));
MarginInfo margin = new MarginInfo();
margin.setLeft(50);
margin.setTop(20);
header.setMargin(margin);
footer.setMargin(margin);
try (Document document = new Document(inputFile.toString())) {
for (int i = 1; i <= document.getPages().size(); i++) {
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}
Add HTML headers and footers
Use this example when header and footer content should include inline HTML formatting.
- Create HeaderFooter objects and add HtmlFragment content.
- Configure margins for placement.
- Assign the header and footer to each page and save the document.
public static void addHeaderAndFooterAsHtml(Path inputFile, Path outputFile) {
HeaderFooter header = new HeaderFooter();
header.getParagraphs().add(new HtmlFragment("This is an HTML <strong>Header</strong>"));
HeaderFooter footer = new HeaderFooter();
footer.getParagraphs().add(new HtmlFragment("Powered by <i>Aspose.PDF</i>"));
MarginInfo margin = new MarginInfo();
margin.setLeft(50);
margin.setTop(20);
header.setMargin(margin);
footer.setMargin(margin);
try (Document document = new Document(inputFile.toString())) {
for (int i = 1; i <= document.getPages().size(); i++) {
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}
Add image headers and footers
Use this example when the header and footer should display an image on every page.
- Create Image objects and add them to header and footer containers.
- Configure margins and assign the containers to each page.
- Save the updated PDF.
public static void addHeaderAndFooterAsImage(Path inputFile, Path imageFile, Path outputFile) {
Image headerImage = new Image();
headerImage.setFile(imageFile.toString());
HeaderFooter header = new HeaderFooter();
header.getParagraphs().add(headerImage);
Image footerImage = new Image();
footerImage.setFile(imageFile.toString());
HeaderFooter footer = new HeaderFooter();
footer.getParagraphs().add(footerImage);
try (Document document = new Document(inputFile.toString())) {
for (int i = 1; i <= document.getPages().size(); i++) {
MarginInfo margin = new MarginInfo();
margin.setLeft(50);
header.setMargin(margin);
footer.setMargin(margin);
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}
Add table-based headers and footers
Use this example when header and footer content should use table layout and text styling.
- Create the required text styles and table objects.
- Add the tables to HeaderFooter containers.
- Apply the header and footer to each page and save the document.
public static void addHeaderAndFooterAsTable(Path inputFile, Path outputFile) {
TextState textStateHeader = new TextState();
textStateHeader.setFont(FontRepository.findFont("Arial"));
textStateHeader.setFontSize(12);
textStateHeader.setHorizontalAlignment(HorizontalAlignment.Center);
TextState textStateFooter = new TextState();
textStateFooter.setFont(FontRepository.findFont("Arial"));
textStateFooter.setFontSize(12);
textStateFooter.setHorizontalAlignment(HorizontalAlignment.Left);
HeaderFooter header = new HeaderFooter();
HeaderFooter footer = new HeaderFooter();
Table tableHeader = new Table();
tableHeader.setColumnWidths(String.valueOf(594 - header.getMargin().getLeft() - header.getMargin().getRight()));
tableHeader.getRows().add().getCells().add("This is a Table Header", textStateHeader);
Table table = new Table();
table.setColumnWidths(String.valueOf(594 - footer.getMargin().getLeft() - footer.getMargin().getRight()));
table.getRows().add().getCells().add("Powered by Aspose.PDF", textStateFooter);
header.getParagraphs().add(tableHeader);
footer.getParagraphs().add(table);
footer.getMargin().setLeft(150);
try (Document document = new Document(inputFile.toString())) {
for (int i = 1; i <= document.getPages().size(); i++) {
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}
Add LaTeX headers and footers
Use this example when the header and footer should render TeX or LaTeX content.
- Open the source PDF and determine the total page count.
- Create TeXFragment content for the header and footer of each page.
- Assign the content and save the document.
public static void addHeaderAndFooterAsLatex(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
int pageCount = document.getPages().size();
for (int i = 1; i <= pageCount; i++) {
HeaderFooter header = new HeaderFooter();
header.getParagraphs().add(new TeXFragment("This is a LaTeX Header. \\today\\", true));
HeaderFooter footer = new HeaderFooter();
footer.getParagraphs().add(new TeXFragment("\\copyright\\ 2025 My Company -- Page \\thepage\\ is " + pageCount, true));
document.getPages().get_Item(i).setHeader(header);
document.getPages().get_Item(i).setFooter(footer);
}
document.save(outputFile.toString());
}
}