Add PDF Headers and Footers in Java

Header and footer artifacts are non-content pagination elements commonly used for repeated labels, page identifiers, and layout framing.

Create a header artifact

Use this helper when you need a reusable header artifact with consistent text styling and alignment.

  1. Create a HeaderArtifact.
  2. Set its text, font settings, and foreground color.
  3. Configure the horizontal alignment and return the artifact.
public static HeaderArtifact createHeaderArtifact(String text) {
    HeaderArtifact artifact = new HeaderArtifact();
    artifact.setText(text);
    artifact.getTextState().setFontSize(14);
    artifact.getTextState().setFont(FontRepository.findFont("Arial"));
    artifact.getTextState().setForegroundColor(Color.getNavy());
    artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
    return artifact;
}

This helper creates a reusable footer artifact with the same styling pattern as the header artifact.

  1. Create a FooterArtifact.
  2. Set its text, text state, and foreground color.
  3. Configure the alignment and return the artifact.
public static FooterArtifact createFooterArtifact(String text) {
    FooterArtifact artifact = new FooterArtifact();
    artifact.setText(text);
    artifact.getTextState().setFontSize(14);
    artifact.getTextState().setFont(FontRepository.findFont("Arial"));
    artifact.getTextState().setForegroundColor(Color.getNavy());
    artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
    return artifact;
}

Add a header artifact

Use this example when a page should display a reusable header artifact.

  1. Open the source PDF Document.
  2. Create the header artifact through the helper method.
  3. Add the artifact to the page and save the output file.
public static void addHeaderArtifact(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        HeaderArtifact header = createHeaderArtifact("Sample Header");
        document.getPages().get_Item(1).getArtifacts().add(header);
        document.save(outputFile.toString());
    }
}

Use this example when the page should display a footer artifact with reusable formatting.

  1. Open the source PDF Document.
  2. Create the footer artifact through the helper method.
  3. Add the artifact to the page and save the output file.
public static void addFooterArtifact(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        FooterArtifact footer = createFooterArtifact("Sample Footer");
        document.getPages().get_Item(1).getArtifacts().add(footer);
        document.save(outputFile.toString());
    }
}