Manage PDF Headers and Footers using 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());
    }
}

Use this approach when existing header and footer artifacts should be removed from the page.

  1. Open the source PDF Document.
  2. Iterate through the page artifact collection in reverse order.
  3. Delete pagination artifacts whose subtype is header or footer, then save the document.
public static void deleteHeaderFooterArtifact(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (int i = document.getPages().get_Item(1).getArtifacts().size(); i >= 1; i--) {
            Artifact artifact = document.getPages().get_Item(1).getArtifacts().get_Item(i);
            if (artifact.getType() == Artifact.ArtifactType.Pagination
                    && (artifact.getSubtype() == Artifact.ArtifactSubtype.Header
                    || artifact.getSubtype() == Artifact.ArtifactSubtype.Footer)) {
                document.getPages().get_Item(1).getArtifacts().delete(artifact);
            }
        }

        document.save(outputFile.toString());
    }
}