public class HeaderFooterCollection extends NodeCollection
HeaderFooter nodes of a Section.
To learn more, visit the Working with Headers and Footers documentation article.
Remarks:
There can be maximum of one HeaderFooter
HeaderFooterType per Section.
HeaderFooter objects can occur in any order in the collection.
Examples:
Shows how to delete all footers from a document.
Document doc = new Document(getMyDir() + "Header and footer types.docx");
// Iterate through each section and remove footers of every kind.
for (Section section : doc.getSections()) {
// There are three kinds of footer and header types.
// 1 - The "First" header/footer, which only appears on the first page of a section.
HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
if (footer != null) {
footer.remove();
}
// 2 - The "Primary" header/footer, which appears on odd pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer != null) {
footer.remove();
}
// 3 - The "Even" header/footer, which appears on even pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
if (footer != null) {
footer.remove();
}
Assert.assertEquals(0, IterableUtils.countMatches(section.getHeadersFooters(), s -> !s.isHeader()));
}
doc.save(getArtifactsDir() + "HeaderFooter.RemoveFooters.docx");
Shows how to create a header and a footer.
Document doc = new Document();
// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HEADER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(header);
Paragraph para = header.appendParagraph("My header.");
Assert.assertTrue(header.isHeader());
Assert.assertTrue(para.isEndOfHeaderFooter());
// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(footer);
para = footer.appendParagraph("My footer.");
Assert.assertFalse(footer.isHeader());
Assert.assertTrue(para.isEndOfHeaderFooter());
Assert.assertEquals(para.getParentStory(), footer);
Assert.assertEquals(para.getParentSection(), footer.getParentSection());
Assert.assertEquals(header.getParentSection(), footer.getParentSection());
doc.save(getArtifactsDir() + "HeaderFooter.Create.docx");
| Modifier and Type | Method and Description |
|---|---|
Node |
get(int index)
Retrieves a
HeaderFooter at the given index. |
HeaderFooter |
getByHeaderFooterType(int headerFooterType) |
void |
linkToPrevious(boolean isLinkToPrevious)
Links or unlinks all headers and footers to the corresponding headers and footers in the previous section.
|
void |
linkToPrevious(int headerFooterType,
boolean isLinkToPrevious) |
HeaderFooter[] |
toArray()
Copies all
HeaderFooter s from the collection to a new array of HeaderFooter s. |
add, clear, contains, getContainer, getCount, getCurrentNode, getNextMatchingNode, indexOf, insert, iterator, remove, removeAtpublic Node get(int index)
HeaderFooter at the given index.
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
Examples:
Shows how to link headers and footers between sections.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 3");
// Move to the first section and create a header and a footer. By default,
// the header and the footer will only appear on pages in the section that contains them.
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("This is the header, which will be displayed in sections 1 and 2.");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("This is the footer, which will be displayed in sections 1, 2 and 3.");
// We can link a section's headers/footers to the previous section's headers/footers
// to allow the linking section to display the linked section's headers/footers.
doc.getSections().get(1).getHeadersFooters().linkToPrevious(true);
// Each section will still have its own header/footer objects. When we link sections,
// the linking section will display the linked section's header/footers while keeping its own.
Assert.assertNotEquals(doc.getSections().get(0).getHeadersFooters().get(0), doc.getSections().get(1).getHeadersFooters().get(0));
Assert.assertNotEquals(doc.getSections().get(0).getHeadersFooters().get(0).getParentSection(), doc.getSections().get(1).getHeadersFooters().get(0).getParentSection());
// Link the headers/footers of the third section to the headers/footers of the second section.
// The second section already links to the first section's header/footers,
// so linking to the second section will create a link chain.
// The first, second, and now the third sections will all display the first section's headers.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(true);
// We can un-link a previous section's header/footers by passing "false" when calling the LinkToPrevious method.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(false);
// We can also select only a specific type of header/footer to link using this method.
// The third section now will have the same footer as the second and first sections, but not the header.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(HeaderFooterType.FOOTER_PRIMARY, true);
// The first section's header/footers cannot link themselves to anything because there is no previous section.
Assert.assertEquals(2, doc.getSections().get(0).getHeadersFooters().getCount());
Assert.assertEquals(0, IterableUtils.countMatches(doc.getSections().get(0).getHeadersFooters(), s -> s.isLinkedToPrevious()));
// All the second section's header/footers are linked to the first section's headers/footers.
Assert.assertEquals(6, doc.getSections().get(1).getHeadersFooters().getCount());
Assert.assertEquals(6, IterableUtils.countMatches(doc.getSections().get(1).getHeadersFooters(), s -> s.isLinkedToPrevious()));
// In the third section, only the footer is linked to the first section's footer via the second section.
Assert.assertEquals(6, doc.getSections().get(2).getHeadersFooters().getCount());
Assert.assertEquals(1, IterableUtils.countMatches(doc.getSections().get(2).getHeadersFooters(), s -> s.isLinkedToPrevious()));
Assert.assertTrue(doc.getSections().get(2).getHeadersFooters().get(3).isLinkedToPrevious());
doc.save(getArtifactsDir() + "HeaderFooter.Link.docx");
get in class NodeCollectionindex - An index into the collection.HeaderFooter value.public HeaderFooter getByHeaderFooterType(int headerFooterType)
public void linkToPrevious(boolean isLinkToPrevious)
Remarks:
If any of the headers or footers do not exist, creates them automatically.
Examples:
Shows how to link headers and footers between sections.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 2");
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.write("Section 3");
// Move to the first section and create a header and a footer. By default,
// the header and the footer will only appear on pages in the section that contains them.
builder.moveToSection(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("This is the header, which will be displayed in sections 1 and 2.");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.write("This is the footer, which will be displayed in sections 1, 2 and 3.");
// We can link a section's headers/footers to the previous section's headers/footers
// to allow the linking section to display the linked section's headers/footers.
doc.getSections().get(1).getHeadersFooters().linkToPrevious(true);
// Each section will still have its own header/footer objects. When we link sections,
// the linking section will display the linked section's header/footers while keeping its own.
Assert.assertNotEquals(doc.getSections().get(0).getHeadersFooters().get(0), doc.getSections().get(1).getHeadersFooters().get(0));
Assert.assertNotEquals(doc.getSections().get(0).getHeadersFooters().get(0).getParentSection(), doc.getSections().get(1).getHeadersFooters().get(0).getParentSection());
// Link the headers/footers of the third section to the headers/footers of the second section.
// The second section already links to the first section's header/footers,
// so linking to the second section will create a link chain.
// The first, second, and now the third sections will all display the first section's headers.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(true);
// We can un-link a previous section's header/footers by passing "false" when calling the LinkToPrevious method.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(false);
// We can also select only a specific type of header/footer to link using this method.
// The third section now will have the same footer as the second and first sections, but not the header.
doc.getSections().get(2).getHeadersFooters().linkToPrevious(HeaderFooterType.FOOTER_PRIMARY, true);
// The first section's header/footers cannot link themselves to anything because there is no previous section.
Assert.assertEquals(2, doc.getSections().get(0).getHeadersFooters().getCount());
Assert.assertEquals(0, IterableUtils.countMatches(doc.getSections().get(0).getHeadersFooters(), s -> s.isLinkedToPrevious()));
// All the second section's header/footers are linked to the first section's headers/footers.
Assert.assertEquals(6, doc.getSections().get(1).getHeadersFooters().getCount());
Assert.assertEquals(6, IterableUtils.countMatches(doc.getSections().get(1).getHeadersFooters(), s -> s.isLinkedToPrevious()));
// In the third section, only the footer is linked to the first section's footer via the second section.
Assert.assertEquals(6, doc.getSections().get(2).getHeadersFooters().getCount());
Assert.assertEquals(1, IterableUtils.countMatches(doc.getSections().get(2).getHeadersFooters(), s -> s.isLinkedToPrevious()));
Assert.assertTrue(doc.getSections().get(2).getHeadersFooters().get(3).isLinkedToPrevious());
doc.save(getArtifactsDir() + "HeaderFooter.Link.docx");
isLinkToPrevious - true to link the headers and footers to the previous section; false to unlink them.public void linkToPrevious(int headerFooterType,
boolean isLinkToPrevious)
public HeaderFooter[] toArray()
HeaderFooter s from the collection to a new array of HeaderFooter s.
Examples:
Shows how to print the node structure of every header and footer in a document.
public void headerFooterToText() throws Exception {
Document doc = new Document(getMyDir() + "DocumentVisitor-compatible features.docx");
HeaderFooterStructurePrinter visitor = new HeaderFooterStructurePrinter();
// When we get a composite node to accept a document visitor, the visitor visits the accepting node,
// and then traverses all the node's children in a depth-first manner.
// The visitor can read and modify each visited node.
doc.accept(visitor);
System.out.println(visitor.getText());
// An alternative way of accessing a document's header/footers section-by-section is by accessing the collection.
HeaderFooter[] headerFooters = doc.getFirstSection().getHeadersFooters().toArray();
Assert.assertEquals(3, headerFooters.length);
}
/// <summary>
/// Traverses a node's non-binary tree of child nodes.
/// Creates a map in the form of a string of all encountered HeaderFooter nodes and their children.
/// </summary>
public static class HeaderFooterStructurePrinter extends DocumentVisitor {
public HeaderFooterStructurePrinter() {
mBuilder = new StringBuilder();
mVisitorIsInsideHeaderFooter = false;
}
public String getText() {
return mBuilder.toString();
}
/// <summary>
/// Called when a Run node is encountered in the document.
/// </summary>
public int visitRun(final Run run) {
if (mVisitorIsInsideHeaderFooter) indentAndAppendLine("[Run] \"" + run.getText() + "\"");
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called when a HeaderFooter node is encountered in the document.
/// </summary>
public int visitHeaderFooterStart(final HeaderFooter headerFooter) {
indentAndAppendLine("[HeaderFooter start] HeaderFooterType: " + headerFooter.getHeaderFooterType());
mDocTraversalDepth++;
mVisitorIsInsideHeaderFooter = true;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Called after all the child nodes of a HeaderFooter node have been visited.
/// </summary>
public int visitHeaderFooterEnd(final HeaderFooter headerFooter) {
mDocTraversalDepth--;
indentAndAppendLine("[HeaderFooter end]");
mVisitorIsInsideHeaderFooter = false;
return VisitorAction.CONTINUE;
}
/// <summary>
/// Append a line to the StringBuilder, and indent it depending on how deep the visitor is into the document tree.
/// </summary>
/// <param name="text"></param>
private void indentAndAppendLine(final String text) {
for (int i = 0; i < mDocTraversalDepth; i++) {
mBuilder.append("| ");
}
mBuilder.append(text + "\r\n");
}
private boolean mVisitorIsInsideHeaderFooter;
private int mDocTraversalDepth;
private final StringBuilder mBuilder;
}
toArray in class NodeCollectionHeaderFooter s.