public class FieldSectionPages extends Field
To learn more, visit the Working with Fields documentation article.
Remarks:
Retrieves the number of the current page within the current section.
Examples:
Shows how to use SECTION and SECTIONPAGES fields to number pages by sections.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
// A SECTION field displays the number of the section it is in.
builder.write("Section ");
FieldSection fieldSection = (FieldSection) builder.insertField(FieldType.FIELD_SECTION, true);
Assert.assertEquals(fieldSection.getFieldCode(), " SECTION ");
// A PAGE field displays the number of the page it is in.
builder.write("\nPage ");
FieldPage fieldPage = (FieldPage) builder.insertField(FieldType.FIELD_PAGE, true);
Assert.assertEquals(fieldPage.getFieldCode(), " PAGE ");
// A SECTIONPAGES field displays the number of pages that the section it is in spans across.
builder.write(" of ");
FieldSectionPages fieldSectionPages = (FieldSectionPages) builder.insertField(FieldType.FIELD_SECTION_PAGES, true);
Assert.assertEquals(fieldSectionPages.getFieldCode(), " SECTIONPAGES ");
// Move out of the header back into the main document and insert two pages.
// All these pages will be in the first section. Our fields, which appear once every header,
// will number the current/total pages of this section.
builder.moveToDocumentEnd();
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertBreak(BreakType.PAGE_BREAK);
// We can insert a new section with the document builder like this.
// This will affect the values displayed in the SECTION and SECTIONPAGES fields in all upcoming headers.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// The PAGE field will keep counting pages across the whole document.
// We can manually reset its count at each section to keep track of pages section-by-section.
builder.getCurrentSection().getPageSetup().setRestartPageNumbering(true);
builder.insertBreak(BreakType.PAGE_BREAK);
doc.updateFields();
doc.save(getArtifactsDir() + "Field.SECTION.SECTIONPAGES.docx");
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, update