java.lang.ObjectNodeCollection
com.aspose.words.HeaderFooterCollection
public class HeaderFooterCollection
There can be maximum of one HeaderFooter HeaderFooter objects can occur in any order in the collection. Example: Example:
Document doc = new Document();
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY);
doc.getFirstSection().getHeadersFooters().add(footer);
Paragraph para = new Paragraph(doc);
footer.getParagraphs().add(para);
Run run = new Run(doc, "TEST FOOTER");
para.getRuns().add(run);
doc.save(getMyDir() + "HeaderFooter.CreateFooter Out.doc");
Document doc = new Document(getMyDir() + "HeaderFooter.RemoveFooters.doc");
for (Node sectionNode : doc)
{
Section section = (Section)sectionNode;
// Up to three different footers are possible in a section (for first, even and odd pages).
// We check and delete all of them.
HeaderFooter footer;
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST);
if (footer != null)
footer.remove();
// Primary footer is the footer used for odd pages.
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
if (footer != null)
footer.remove();
footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN);
if (footer != null)
footer.remove();
}
doc.save(getMyDir() + "HeaderFooter.RemoveFooters Out.doc");
Property Getters/Setters Summary | ||
---|---|---|
int | getCount() | → inherited from NodeCollection |
Gets the number of nodes in the collection. | ||
HeaderFooter | get(int index) | |
Retrieves a HeaderFooter at the given index. | ||
HeaderFooter | getByHeaderFooterType(int headerFooterType) | |
Retrieves a HeaderFooter of the specified type. |
Method Summary | ||
---|---|---|
void | add(Node node) | → inherited from NodeCollection |
Adds a node to the end of the collection. | ||
void | clear() | → inherited from NodeCollection |
Removes all nodes from this collection and from the document. | ||
boolean | contains(Node node) | → inherited from NodeCollection |
Determines whether a node is in the collection. | ||
int | indexOf(Node node) | → inherited from NodeCollection |
Returns the zero-based index of the specified node. | ||
void | insert(int index, Node node) | → inherited from NodeCollection |
Inserts a node into the collection at the specified index. | ||
java.util.Iterator | iterator() | → inherited from NodeCollection |
Provides a simple "foreach" style iteration over the collection of nodes. | ||
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) | |
Links or unlinks the specified header or footer to the corresponding header or footer in the previous section. | ||
void | remove(Node node) | → inherited from NodeCollection |
Removes the node from the collection and from the document. | ||
void | removeAt(int index) | → inherited from NodeCollection |
Removes the node at the specified index from the collection and from the document. | ||
Aspose.Words.Node[] | toArray() | → inherited from NodeCollection |
Copies all nodes from the collection to a new array of nodes. |
Property Getters/Setters Detail |
---|
getCount | → inherited from NodeCollection |
public int getCount() |
Example:
Shows how to enumerate immediate children of a CompositeNode using indexed access.NodeCollection children = paragraph.getChildNodes(); for (int i = 0; i < children.getCount(); i++) { Node child = children.get(i); // Paragraph may contain children of various types such as runs, shapes and so on. if (child.getNodeType() == NodeType.RUN) { // Say we found the node that we want, do something useful. Run run = (Run)child; System.out.println(run.getText()); } }
get | |
public HeaderFooter get(int index) |
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.
index
- An index into the collection.getByHeaderFooterType | |
public HeaderFooter getByHeaderFooterType(int headerFooterType) |
headerFooterType
- A Example:
Deletes all footers from all sections, but leaves headers intact.Document doc = new Document(getMyDir() + "HeaderFooter.RemoveFooters.doc"); for (Node sectionNode : doc) { Section section = (Section)sectionNode; // Up to three different footers are possible in a section (for first, even and odd pages). // We check and delete all of them. HeaderFooter footer; footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_FIRST); if (footer != null) footer.remove(); // Primary footer is the footer used for odd pages. footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); if (footer != null) footer.remove(); footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_EVEN); if (footer != null) footer.remove(); } doc.save(getMyDir() + "HeaderFooter.RemoveFooters Out.doc");
Example:
Shows how to replace text in the document footer.// Open the template document, containing obsolete copyright information in the footer. Document doc = new Document(getMyDir() + "HeaderFooter.ReplaceText.doc"); HeaderFooterCollection headersFooters = doc.getFirstSection().getHeadersFooters(); HeaderFooter footer = headersFooters.getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY); footer.getRange().replace("(C) 2006 Aspose Pty Ltd.", "Copyright (C) 2008 by Aspose Pty Ltd.", false, false); doc.save(getMyDir() + "HeaderFooter.ReplaceText Out.doc");
Method Detail |
---|
linkToPrevious | |
public void linkToPrevious(boolean isLinkToPrevious) throws java.lang.Exception |
If any of the headers or footers do not exist, creates them automatically.
isLinkToPrevious
- True to link the headers and footers to the previous section;
false to unlink them.linkToPrevious | |
public void linkToPrevious(int headerFooterType, boolean isLinkToPrevious) throws java.lang.Exception |
If the header or footer of the specified type does not exist, creates it automatically.
headerFooterType
- A isLinkToPrevious
- True to link the header or footer to the previous section;
false to unlink.add | → inherited from NodeCollection |
public void add(Node node) throws java.lang.Exception |
The node is inserted as a child into the node object from which the collection was created.
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
node
- The node to be added to the end of the collection.Example:
Creates a footer using the document object model and inserts it into a section.Document doc = new Document(); HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FOOTER_PRIMARY); doc.getFirstSection().getHeadersFooters().add(footer); Paragraph para = new Paragraph(doc); footer.getParagraphs().add(para); Run run = new Run(doc, "TEST FOOTER"); para.getRuns().add(run); doc.save(getMyDir() + "HeaderFooter.CreateFooter Out.doc");
insert | → inherited from NodeCollection |
public void insert(int index, Node node) throws java.lang.Exception |
The node is inserted as a child into the node object from which the collection was created.
If the index is equal to or greater than Count, the node is added at the end of the collection.
If the index is negative and its absolute value is greater than Count, the node is added at the end of the collection.
If the newChild is already in the tree, it is first removed.
If the node being inserted was created from another document, you should use
index
- The zero-based index of the node.
Negative indexes are allowed and indicate access from the back of the list.
For example -1 means the last node, -2 means the second before last and so on.node
- The node to insert.remove | → inherited from NodeCollection |
public void remove(Node node) throws java.lang.Exception |
node
- The node to remove.removeAt | → inherited from NodeCollection |
public void removeAt(int index) throws java.lang.Exception |
index
- The zero-based index of the node.
Negative indexes are allowed and indicate access from the back of the list.
For example -1 means the last node, -2 means the second before last and so on.clear | → inherited from NodeCollection |
public void clear() throws java.lang.Exception |
contains | → inherited from NodeCollection |
public boolean contains(Node node) |
This method performs a linear search; therefore, the average execution time is proportional to Count.
node
- The node to locate.indexOf | → inherited from NodeCollection |
public int indexOf(Node node) |
This method performs a linear search; therefore, the average execution time is proportional to Count.
node
- The node to locate.toArray | → inherited from NodeCollection |
public Aspose.Words.Node[] toArray() |
You should not be adding/removing nodes while iterating over a collection of nodes because it invalidates the iterator and requires refreshes for live collections.
To be able to add/remove nodes during iteration, use this method to copy nodes into a fixed-size array and then iterate over the array.
Example:
Shows how to replace all textboxes with images.Document doc = new Document(getMyDir() + "Shape.ReplaceTextboxesWithImages.doc"); // This gets a live collection of all shape nodes in the document. NodeCollection shapeCollection = doc.getChildNodes(NodeType.SHAPE, true); // Since we will be adding/removing nodes, it is better to copy all collection // into a fixed size array, otherwise iterator will be invalidated. Node[] shapes = shapeCollection.toArray(); for (Node shapeNodes : shapes) { Shape shape = (Shape) shapeNodes; // Filter out all shapes that we don't need. if (shape.getShapeType() == ShapeType.TEXT_BOX) { // Create a new shape that will replace the existing shape. Shape image = new Shape(doc, ShapeType.IMAGE); // Load the image into the new shape. image.getImageData().setImage(getMyDir() + "Hammer.wmf"); // Make new shape's position to match the old shape. image.setLeft(shape.getLeft()); image.setTop(shape.getTop()); image.setWidth(shape.getWidth()); image.setHeight(shape.getHeight()); image.setRelativeHorizontalPosition(shape.getRelativeHorizontalPosition()); image.setRelativeVerticalPosition(shape.getRelativeVerticalPosition()); image.setHorizontalAlignment(shape.getHorizontalAlignment()); image.setVerticalAlignment(shape.getVerticalAlignment()); image.setWrapType(shape.getWrapType()); image.setWrapSide(shape.getWrapSide()); // Insert new shape after the old shape and remove the old shape. shape.getParentNode().insertAfter(image, shape); shape.remove(); } } doc.save(getMyDir() + "Shape.ReplaceTextboxesWithImages Out.doc");
iterator | → inherited from NodeCollection |
public java.util.Iterator iterator() |