|
java.lang.Object
Node
CompositeNode
Story
com.aspose.words.HeaderFooter
- All Implemented Interfaces:
- java.lang.Iterable, java.lang.Iterable, java.lang.Cloneable
public class HeaderFooter - extends Story
Represents a container for the header or footer text of a section.
HeaderFooter can contain Paragraph and Table child nodes. HeaderFooter is a section-level node and can only be a child of Section.
There can only be one HeaderFooter or each HeaderFooterType in a Section. If Section does not have a HeaderFooter of a specific type or
the HeaderFooter has no child nodes, this header/footer is considered linked to
the header/footer of the same type of the previous section in Microsoft Word. When HeaderFooter contains at least one one Paragraph, it is no longer
considered linked to previous in Microsoft Word. Example: Shows how to remove headers/footers from a section.
section.clearHeadersFooters(); Example: Shows how to clear section content but preserve headers/footers.
section.getBody().removeAllChildren();
section.getBody().ensureMinimum(); 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.Create Out.doc");
// In the copyright info replace year number from 2005 to 2006.
doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY).getRange().replace("(C) 2005 Aspose Pty Ltd.", "(C) 2006 Aspose Pty Ltd.", true, true);
doc.save(getMyDir() + "HeaderFooter.Change Out.doc");Example: Shows how to create header and footer in the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Get page setup for the current section.
PageSetup pageSetup = builder.getPageSetup();
// Set to 'true' if you want different headers/footers for first, even and odd pages.
pageSetup.setDifferentFirstPageHeaderFooter(false);
pageSetup.setOddAndEvenPagesHeaderFooter(false);
pageSetup.setHeaderDistance(20);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
String imageFileName = getMyDir() + "Aspose.Words.gif";
Shape shape = builder.insertImage(imageFileName);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setLeft(10);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setTop(10);
shape.setWidth(50);
shape.setHeight(50);
shape.setWrapType(WrapType.THROUGH);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("ASPOSE.WORDS DYNAMIC TEMPLATE");
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
// We use table with two cells to make one part of the text on the line (with page numbering)
// to be aligned left, and the other part of the text (with copyright) to be aligned right.
builder.startTable();
// Calculate table width as total page width with left and right marins subtracted.
double tableWidth = pageSetup.getPageWidth() - pageSetup.getLeftMargin() - pageSetup.getRightMargin();
// Set the font properties for the footer.
builder.getFont().setSize(12);
builder.getFont().setName("Arial");
builder.insertCell();
// Set first cell to 1/3 of the page width.
builder.getCellFormat().setWidth(tableWidth / 3);
// Insert page numbering text here.
// It uses PAGE and NUMPAGES fields to autocalculate current page number and total number of pages.
builder.write("Page ");
builder.insertField("PAGE", "");
builder.write(" of ");
builder.insertField("NUMPAGES", "");
// Align this text to the left.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
builder.insertCell();
// Set the second cell to 2/3 of the page width.
builder.getCellFormat().setWidth(tableWidth * 2 / 3);
builder.write("(C) 2006 Aspose Pty Ltd. All rights reserved.");
// Align this text to the right.
builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.endRow();
builder.endTable();
doc.save(getMyDir() + "HeaderFooter.Create Out.doc");
|
Constructor Summary |
HeaderFooter(Document doc, int headerFooterType)
Creates a new header or footer of the specified type.
|
|