FieldToc
Inheritance: java.lang.Object, com.aspose.words.Field
public class FieldToc extends Field
Implements the TOC field.
To learn more, visit the Working with Fields documentation article.
Remarks:
Builds a table of contents (which can also be a table of figures) using the entries specified by TC fields, their heading levels, and specified styles, and inserts that table at this place in the document.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Methods
Method | Description |
---|---|
getBookmarkName() | Gets the name of the bookmark that marks the portion of the document used to build the table. |
getCaptionlessTableOfFiguresLabel() | Gets the name of the sequence identifier used when building a table of figures that does not include caption’s label and number. |
getCustomStyles() | Gets a list of styles other than the built-in heading styles to include in the table of contents. |
getDisplayResult() | Gets the text that represents the displayed field result. |
getEnd() | Gets the node that represents the field end. |
getEntryIdentifier() | Gets a string that should match type identifiers of TC fields being included. |
getEntryLevelRange() | Gets a range of levels of the table of contents entries to be included. |
getEntrySeparator() | Gets a sequence of characters that separate an entry and its page number. |
getFieldCode() | Returns text between field start and field separator (or field end if there is no separator). |
getFieldCode(boolean includeChildFieldCodes) | Returns text between field start and field separator (or field end if there is no separator). |
getFormat() | Gets a FieldFormat object that provides typed access to field’s formatting. |
getHeadingLevelRange() | Gets a range of heading levels to include. |
getHideInWebLayout() | Gets whether to hide tab leader and page numbers in Web layout view. |
getInsertHyperlinks() | Gets whether to make the table of contents entries hyperlinks. |
getLocaleId() | Gets the LCID of the field. |
getPageNumberOmittingLevelRange() | Gets a range of levels of the table of contents entries from which to omits page numbers. |
getPrefixedSequenceIdentifier() | Gets the identifier of a sequence for which a prefix should be added to the entry’s page number. |
getPreserveLineBreaks() | Gets whether to preserve newline characters within table entries. |
getPreserveTabs() | Gets whether to preserve tab entries within table entries. |
getResult() | Gets text that is between the field separator and field end. |
getSeparator() | Gets the node that represents the field separator. |
getSequenceSeparator() | Gets the character sequence that is used to separate sequence numbers and page numbers. |
getStart() | Gets the node that represents the start of the field. |
getSwitchType(String switchName) | |
getTableOfFiguresLabel() | Gets the name of the sequence identifier used when building a table of figures. |
getType() | Gets the Microsoft Word field type. |
getUseParagraphOutlineLevel() | Gets whether to use the applied paragraph outline level. |
isDirty() | Gets whether the current result of the field is no longer correct (stale) due to other modifications made to the document. |
isDirty(boolean value) | Sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document. |
isLocked() | Gets whether the field is locked (should not recalculate its result). |
isLocked(boolean value) | Sets whether the field is locked (should not recalculate its result). |
remove() | Removes the field from the document. |
setBookmarkName(String value) | Sets the name of the bookmark that marks the portion of the document used to build the table. |
setCaptionlessTableOfFiguresLabel(String value) | Sets the name of the sequence identifier used when building a table of figures that does not include caption’s label and number. |
setCustomStyles(String value) | Sets a list of styles other than the built-in heading styles to include in the table of contents. |
setEntryIdentifier(String value) | Sets a string that should match type identifiers of TC fields being included. |
setEntryLevelRange(String value) | Sets a range of levels of the table of contents entries to be included. |
setEntrySeparator(String value) | Sets a sequence of characters that separate an entry and its page number. |
setHeadingLevelRange(String value) | Sets a range of heading levels to include. |
setHideInWebLayout(boolean value) | Sets whether to hide tab leader and page numbers in Web layout view. |
setInsertHyperlinks(boolean value) | Sets whether to make the table of contents entries hyperlinks. |
setLocaleId(int value) | Sets the LCID of the field. |
setPageNumberOmittingLevelRange(String value) | Sets a range of levels of the table of contents entries from which to omits page numbers. |
setPrefixedSequenceIdentifier(String value) | Sets the identifier of a sequence for which a prefix should be added to the entry’s page number. |
setPreserveLineBreaks(boolean value) | Sets whether to preserve newline characters within table entries. |
setPreserveTabs(boolean value) | Sets whether to preserve tab entries within table entries. |
setResult(String value) | Sets text that is between the field separator and field end. |
setSequenceSeparator(String value) | Sets the character sequence that is used to separate sequence numbers and page numbers. |
setTableOfFiguresLabel(String value) | Sets the name of the sequence identifier used when building a table of figures. |
setUseParagraphOutlineLevel(boolean value) | Sets whether to use the applied paragraph outline level. |
unlink() | Performs the field unlink. |
update() | Performs the field update. |
update(boolean ignoreMergeFormat) | Performs a field update. |
updatePageNumbers() | Updates the page numbers for items in this table of contents. |
getBookmarkName()
public String getBookmarkName()
Gets the name of the bookmark that marks the portion of the document used to build the table.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: java.lang.String - The name of the bookmark that marks the portion of the document used to build the table.
getCaptionlessTableOfFiguresLabel()
public String getCaptionlessTableOfFiguresLabel()
Gets the name of the sequence identifier used when building a table of figures that does not include caption’s label and number.
Returns: java.lang.String - The name of the sequence identifier used when building a table of figures that does not include caption’s label and number.
getCustomStyles()
public String getCustomStyles()
Gets a list of styles other than the built-in heading styles to include in the table of contents.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: java.lang.String - A list of styles other than the built-in heading styles to include in the table of contents.
getDisplayResult()
public String getDisplayResult()
Gets the text that represents the displayed field result.
Remarks:
The Document.updateListLabels() method must be called to obtain correct value for the FieldListNum, FieldAutoNum, FieldAutoNumOut and FieldAutoNumLgl fields.
Examples:
Shows how to get the real text that a field displays in the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("This document was written by ");
FieldAuthor fieldAuthor = (FieldAuthor) builder.insertField(FieldType.FIELD_AUTHOR, true);
fieldAuthor.setAuthorName("John Doe");
// We can use the DisplayResult property to verify what exact text
// a field would display in its place in the document.
Assert.assertEquals("", fieldAuthor.getDisplayResult());
// Fields do not maintain accurate result values in real-time.
// To make sure our fields display accurate results at any given time,
// such as right before a save operation, we need to update them manually.
fieldAuthor.update();
Assert.assertEquals("John Doe", fieldAuthor.getDisplayResult());
doc.save(getArtifactsDir() + "Field.DisplayResult.docx");
Returns: java.lang.String - The text that represents the displayed field result.
getEnd()
public FieldEnd getEnd()
Gets the node that represents the field end.
Examples:
Shows how to work with a collection of fields.
public void fieldCollection() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder.insertField(" TIME ");
builder.insertField(" REVNUM ");
builder.insertField(" AUTHOR \"John Doe\" ");
builder.insertField(" SUBJECT \"My Subject\" ");
builder.insertField(" QUOTE \"Hello world!\" ");
doc.updateFields();
FieldCollection fields = doc.getRange().getFields();
Assert.assertEquals(6, fields.getCount());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
FieldVisitor fieldVisitor = new FieldVisitor();
Iterator fieldEnumerator = fields.iterator();
while (fieldEnumerator.hasNext()) {
if (fieldEnumerator != null) {
Field currentField = fieldEnumerator.next();
currentField.getStart().accept(fieldVisitor);
if (currentField.getSeparator() != null) {
currentField.getSeparator().accept(fieldVisitor);
}
currentField.getEnd().accept(fieldVisitor);
} else {
System.out.println("There are no fields in the document.");
}
}
System.out.println(fieldVisitor.getText());
}
///
/// Document visitor implementation that prints field info.
///
public static class FieldVisitor extends DocumentVisitor {
public FieldVisitor() {
mBuilder = new StringBuilder();
}
///
/// Gets the plain text of the document that was accumulated by the visitor.
///
public String getText() {
return mBuilder.toString();
}
///
/// Called when a FieldStart node is encountered in the document.
///
public int visitFieldStart(final FieldStart fieldStart) {
mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldSeparator node is encountered in the document.
///
public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldEnd node is encountered in the document.
///
public int visitFieldEnd(final FieldEnd fieldEnd) {
mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
}
Returns: FieldEnd - The node that represents the field end.
getEntryIdentifier()
public String getEntryIdentifier()
Gets a string that should match type identifiers of TC fields being included.
Examples:
Shows how to insert a TOC field, and filter which TC fields end up as entries.
public void fieldTocEntryIdentifier() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a TOC field, which will compile all TC fields into a table of contents.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Configure the field only to pick up TC entries of the "A" type, and an entry-level between 1 and 3.
fieldToc.setEntryIdentifier("A");
fieldToc.setEntryLevelRange("1-3");
Assert.assertEquals(" TOC \\f A \\l 1-3", fieldToc.getFieldCode());
// These two entries will appear in the table.
builder.insertBreak(BreakType.PAGE_BREAK);
insertTocEntry(builder, "TC field 1", "A", "1");
insertTocEntry(builder, "TC field 2", "A", "2");
Assert.assertEquals(" TC \"TC field 1\" \\n \\f A \\l 1", doc.getRange().getFields().get(1).getFieldCode());
// This entry will be omitted from the table because it has a different type from "A".
insertTocEntry(builder, "TC field 3", "B", "1");
// This entry will be omitted from the table because it has an entry-level outside of the 1-3 range.
insertTocEntry(builder, "TC field 4", "A", "5");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TC.docx");
}
///
/// Use a document builder to insert a TC field.
///
public void insertTocEntry(final DocumentBuilder builder, final String text, final String typeIdentifier, final String entryLevel) throws Exception {
FieldTC fieldTc = (FieldTC) builder.insertField(FieldType.FIELD_TOC_ENTRY, true);
fieldTc.setOmitPageNumber(true);
fieldTc.setText(text);
fieldTc.setTypeIdentifier(typeIdentifier);
fieldTc.setEntryLevel(entryLevel);
}
Returns: java.lang.String - A string that should match type identifiers of TC fields being included.
getEntryLevelRange()
public String getEntryLevelRange()
Gets a range of levels of the table of contents entries to be included.
Examples:
Shows how to insert a TOC field, and filter which TC fields end up as entries.
public void fieldTocEntryIdentifier() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a TOC field, which will compile all TC fields into a table of contents.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Configure the field only to pick up TC entries of the "A" type, and an entry-level between 1 and 3.
fieldToc.setEntryIdentifier("A");
fieldToc.setEntryLevelRange("1-3");
Assert.assertEquals(" TOC \\f A \\l 1-3", fieldToc.getFieldCode());
// These two entries will appear in the table.
builder.insertBreak(BreakType.PAGE_BREAK);
insertTocEntry(builder, "TC field 1", "A", "1");
insertTocEntry(builder, "TC field 2", "A", "2");
Assert.assertEquals(" TC \"TC field 1\" \\n \\f A \\l 1", doc.getRange().getFields().get(1).getFieldCode());
// This entry will be omitted from the table because it has a different type from "A".
insertTocEntry(builder, "TC field 3", "B", "1");
// This entry will be omitted from the table because it has an entry-level outside of the 1-3 range.
insertTocEntry(builder, "TC field 4", "A", "5");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TC.docx");
}
///
/// Use a document builder to insert a TC field.
///
public void insertTocEntry(final DocumentBuilder builder, final String text, final String typeIdentifier, final String entryLevel) throws Exception {
FieldTC fieldTc = (FieldTC) builder.insertField(FieldType.FIELD_TOC_ENTRY, true);
fieldTc.setOmitPageNumber(true);
fieldTc.setText(text);
fieldTc.setTypeIdentifier(typeIdentifier);
fieldTc.setEntryLevel(entryLevel);
}
Returns: java.lang.String - A range of levels of the table of contents entries to be included.
getEntrySeparator()
public String getEntrySeparator()
Gets a sequence of characters that separate an entry and its page number.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: java.lang.String - A sequence of characters that separate an entry and its page number.
getFieldCode()
public String getFieldCode()
Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
Examples:
Shows how to insert a field into a document using a field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Field dateField = builder.insertField("DATE \\* MERGEFORMAT");
Assert.assertEquals(FieldType.FIELD_DATE, dateField.getType());
Assert.assertEquals("DATE \\* MERGEFORMAT", dateField.getFieldCode());
Shows how to get a field’s field code.
// Open a document which contains a MERGEFIELD inside an IF field.
Document doc = new Document(getMyDir() + "Nested fields.docx");
FieldIf fieldIf = (FieldIf) doc.getRange().getFields().get(0);
// There are two ways of getting a field's field code:
// 1 - Omit its inner fields:
Assert.assertEquals(" IF > 0 \" (surplus of ) \" \"\" ", fieldIf.getFieldCode(false));
// 2 - Include its inner fields:
Assert.assertEquals(" IF MERGEFIELD NetIncome > 0 \" (surplus of MERGEFIELD NetIncome \\f $ ) \" \"\" ",
fieldIf.getFieldCode(true));
// By default, the GetFieldCode method displays inner fields.
Assert.assertEquals(fieldIf.getFieldCode(), fieldIf.getFieldCode(true));
Returns: java.lang.String
getFieldCode(boolean includeChildFieldCodes)
public String getFieldCode(boolean includeChildFieldCodes)
Returns text between field start and field separator (or field end if there is no separator).
Examples:
Shows how to get a field’s field code.
// Open a document which contains a MERGEFIELD inside an IF field.
Document doc = new Document(getMyDir() + "Nested fields.docx");
FieldIf fieldIf = (FieldIf) doc.getRange().getFields().get(0);
// There are two ways of getting a field's field code:
// 1 - Omit its inner fields:
Assert.assertEquals(" IF > 0 \" (surplus of ) \" \"\" ", fieldIf.getFieldCode(false));
// 2 - Include its inner fields:
Assert.assertEquals(" IF MERGEFIELD NetIncome > 0 \" (surplus of MERGEFIELD NetIncome \\f $ ) \" \"\" ",
fieldIf.getFieldCode(true));
// By default, the GetFieldCode method displays inner fields.
Assert.assertEquals(fieldIf.getFieldCode(), fieldIf.getFieldCode(true));
Parameters:
Parameter | Type | Description |
---|---|---|
includeChildFieldCodes | boolean | true if child field codes should be included. |
Returns: java.lang.String
getFormat()
public FieldFormat getFormat()
Gets a FieldFormat object that provides typed access to field’s formatting.
Examples:
Shows how to format field results.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a field that displays a result with no format applied.
Field field = builder.insertField("= 2 + 3");
Assert.assertEquals("= 2 + 3", field.getFieldCode());
Assert.assertEquals("5", field.getResult());
// We can apply a format to a field's result using the field's properties.
// Below are three types of formats that we can apply to a field's result.
// 1 - Numeric format:
FieldFormat format = field.getFormat();
format.setNumericFormat("$###.00");
field.update();
Assert.assertEquals("= 2 + 3 \\# $###.00", field.getFieldCode());
Assert.assertEquals("$ 5.00", field.getResult());
// 2 - Date/time format:
field = builder.insertField("DATE");
format = field.getFormat();
format.setDateTimeFormat("dddd, MMMM dd, yyyy");
field.update();
Assert.assertEquals("DATE \\@ \"dddd, MMMM dd, yyyy\"", field.getFieldCode());
System.out.println("Today's date, in {format.DateTimeFormat} format:\n\t{field.Result}");
// 3 - General format:
field = builder.insertField("= 25 + 33");
format = field.getFormat();
format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().add(GeneralFormat.UPPER);
field.update();
int index = 0;
Iterator generalFormatEnumerator = format.getGeneralFormats().iterator();
while (generalFormatEnumerator.hasNext()) {
int value = generalFormatEnumerator.next();
System.out.println(MessageFormat.format("General format index {0}: {1}", index++, value));
}
Assert.assertEquals("= 25 + 33 \\* roman \\* Upper", field.getFieldCode());
Assert.assertEquals("LVIII", field.getResult());
Assert.assertEquals(2, format.getGeneralFormats().getCount());
Assert.assertEquals(GeneralFormat.LOWERCASE_ROMAN, format.getGeneralFormats().get(0));
// We can remove our formats to revert the field's result to its original form.
format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().removeAt(0);
Assert.assertEquals(0, format.getGeneralFormats().getCount());
field.update();
Assert.assertEquals("= 25 + 33 ", field.getFieldCode());
Assert.assertEquals("58", field.getResult());
Assert.assertEquals(0, format.getGeneralFormats().getCount());
Returns: FieldFormat - A FieldFormat object that provides typed access to field’s formatting.
getHeadingLevelRange()
public String getHeadingLevelRange()
Gets a range of heading levels to include.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: java.lang.String - A range of heading levels to include.
getHideInWebLayout()
public boolean getHideInWebLayout()
Gets whether to hide tab leader and page numbers in Web layout view.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - Whether to hide tab leader and page numbers in Web layout view.
getInsertHyperlinks()
public boolean getInsertHyperlinks()
Gets whether to make the table of contents entries hyperlinks.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - Whether to make the table of contents entries hyperlinks.
getLocaleId()
public int getLocaleId()
Gets the LCID of the field.
Examples:
Shows how to insert a field and work with its locale.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a DATE field, and then print the date it will display.
// Your thread's current culture determines the formatting of the date.
Field field = builder.insertField("DATE");
System.out.println(MessageFormat.format("Today''s date, as displayed in the \"{0}\" culture: {1}", Locale.getDefault().getDisplayLanguage(), field.getResult()));
Assert.assertEquals(1033, field.getLocaleId());
// Changing the culture of our thread will impact the result of the DATE field.
// Another way to get the DATE field to display a date in a different culture is to use its LocaleId property.
// This way allows us to avoid changing the thread's culture to get this effect.
doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);
CultureInfo de = new CultureInfo("de-DE");
field.setLocaleId(1031);
field.update();
System.out.println(MessageFormat.format("Today''s date, as displayed according to the \"{0}\" culture: {1}", Locale.forLanguageTag(LocaleUtil.getLocaleFromLCID(field.getLocaleId())).getDisplayLanguage(), field.getResult()));
Returns: int - The LCID of the field.
getPageNumberOmittingLevelRange()
public String getPageNumberOmittingLevelRange()
Gets a range of levels of the table of contents entries from which to omits page numbers.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: java.lang.String - A range of levels of the table of contents entries from which to omits page numbers.
getPrefixedSequenceIdentifier()
public String getPrefixedSequenceIdentifier()
Gets the identifier of a sequence for which a prefix should be added to the entry’s page number.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Returns: java.lang.String - The identifier of a sequence for which a prefix should be added to the entry’s page number.
getPreserveLineBreaks()
public boolean getPreserveLineBreaks()
Gets whether to preserve newline characters within table entries.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - Whether to preserve newline characters within table entries.
getPreserveTabs()
public boolean getPreserveTabs()
Gets whether to preserve tab entries within table entries.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - Whether to preserve tab entries within table entries.
getResult()
public String getResult()
Gets text that is between the field separator and field end.
Examples:
Shows how to insert a field into a document using a field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Field dateField = builder.insertField("DATE \\* MERGEFORMAT");
Assert.assertEquals(FieldType.FIELD_DATE, dateField.getType());
Assert.assertEquals("DATE \\* MERGEFORMAT", dateField.getFieldCode());
Returns: java.lang.String - Text that is between the field separator and field end.
getSeparator()
public FieldSeparator getSeparator()
Gets the node that represents the field separator. Can be null .
Examples:
Shows how to work with a collection of fields.
public void fieldCollection() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder.insertField(" TIME ");
builder.insertField(" REVNUM ");
builder.insertField(" AUTHOR \"John Doe\" ");
builder.insertField(" SUBJECT \"My Subject\" ");
builder.insertField(" QUOTE \"Hello world!\" ");
doc.updateFields();
FieldCollection fields = doc.getRange().getFields();
Assert.assertEquals(6, fields.getCount());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
FieldVisitor fieldVisitor = new FieldVisitor();
Iterator fieldEnumerator = fields.iterator();
while (fieldEnumerator.hasNext()) {
if (fieldEnumerator != null) {
Field currentField = fieldEnumerator.next();
currentField.getStart().accept(fieldVisitor);
if (currentField.getSeparator() != null) {
currentField.getSeparator().accept(fieldVisitor);
}
currentField.getEnd().accept(fieldVisitor);
} else {
System.out.println("There are no fields in the document.");
}
}
System.out.println(fieldVisitor.getText());
}
///
/// Document visitor implementation that prints field info.
///
public static class FieldVisitor extends DocumentVisitor {
public FieldVisitor() {
mBuilder = new StringBuilder();
}
///
/// Gets the plain text of the document that was accumulated by the visitor.
///
public String getText() {
return mBuilder.toString();
}
///
/// Called when a FieldStart node is encountered in the document.
///
public int visitFieldStart(final FieldStart fieldStart) {
mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldSeparator node is encountered in the document.
///
public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldEnd node is encountered in the document.
///
public int visitFieldEnd(final FieldEnd fieldEnd) {
mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
}
Returns: FieldSeparator - The node that represents the field separator.
getSequenceSeparator()
public String getSequenceSeparator()
Gets the character sequence that is used to separate sequence numbers and page numbers.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Returns: java.lang.String - The character sequence that is used to separate sequence numbers and page numbers.
getStart()
public FieldStart getStart()
Gets the node that represents the start of the field.
Examples:
Shows how to work with a collection of fields.
public void fieldCollection() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder.insertField(" TIME ");
builder.insertField(" REVNUM ");
builder.insertField(" AUTHOR \"John Doe\" ");
builder.insertField(" SUBJECT \"My Subject\" ");
builder.insertField(" QUOTE \"Hello world!\" ");
doc.updateFields();
FieldCollection fields = doc.getRange().getFields();
Assert.assertEquals(6, fields.getCount());
// Iterate over the field collection, and print contents and type
// of every field using a custom visitor implementation.
FieldVisitor fieldVisitor = new FieldVisitor();
Iterator fieldEnumerator = fields.iterator();
while (fieldEnumerator.hasNext()) {
if (fieldEnumerator != null) {
Field currentField = fieldEnumerator.next();
currentField.getStart().accept(fieldVisitor);
if (currentField.getSeparator() != null) {
currentField.getSeparator().accept(fieldVisitor);
}
currentField.getEnd().accept(fieldVisitor);
} else {
System.out.println("There are no fields in the document.");
}
}
System.out.println(fieldVisitor.getText());
}
///
/// Document visitor implementation that prints field info.
///
public static class FieldVisitor extends DocumentVisitor {
public FieldVisitor() {
mBuilder = new StringBuilder();
}
///
/// Gets the plain text of the document that was accumulated by the visitor.
///
public String getText() {
return mBuilder.toString();
}
///
/// Called when a FieldStart node is encountered in the document.
///
public int visitFieldStart(final FieldStart fieldStart) {
mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldSeparator node is encountered in the document.
///
public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
return VisitorAction.CONTINUE;
}
///
/// Called when a FieldEnd node is encountered in the document.
///
public int visitFieldEnd(final FieldEnd fieldEnd) {
mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
return VisitorAction.CONTINUE;
}
private final StringBuilder mBuilder;
}
Returns: FieldStart - The node that represents the start of the field.
getSwitchType(String switchName)
public int getSwitchType(String switchName)
Parameters:
Parameter | Type | Description |
---|---|---|
switchName | java.lang.String |
Returns: int
getTableOfFiguresLabel()
public String getTableOfFiguresLabel()
Gets the name of the sequence identifier used when building a table of figures.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Returns: java.lang.String - The name of the sequence identifier used when building a table of figures.
getType()
public int getType()
Gets the Microsoft Word field type.
Examples:
Shows how to insert a field into a document using a field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Field dateField = builder.insertField("DATE \\* MERGEFORMAT");
Assert.assertEquals(FieldType.FIELD_DATE, dateField.getType());
Assert.assertEquals("DATE \\* MERGEFORMAT", dateField.getFieldCode());
Returns: int - The Microsoft Word field type. The returned value is one of FieldType constants.
getUseParagraphOutlineLevel()
public boolean getUseParagraphOutlineLevel()
Gets whether to use the applied paragraph outline level.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - Whether to use the applied paragraph outline level.
isDirty()
public boolean isDirty()
Gets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
Examples:
Shows how to use special property for updating field result.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Give the document's built-in "Author" property value, and then display it with a field.
doc.getBuiltInDocumentProperties().setAuthor("John Doe");
FieldAuthor field = (FieldAuthor) builder.insertField(FieldType.FIELD_AUTHOR, true);
Assert.assertFalse(field.isDirty());
Assert.assertEquals("John Doe", field.getResult());
// Update the property. The field still displays the old value.
doc.getBuiltInDocumentProperties().setAuthor("John & Jane Doe");
Assert.assertEquals("John Doe", field.getResult());
// Since the field's value is out of date, we can mark it as "dirty".
// This value will stay out of date until we update the field manually with the Field.Update() method.
field.isDirty(true);
// If we save without calling an update method,
// the field will keep displaying the out of date value in the output document.
doc.save(getArtifactsDir() + "Filed.UpdateDirtyFields.docx");
// The LoadOptions object has an option to update all fields
// marked as "dirty" when loading the document.
LoadOptions options = new LoadOptions();
options.setUpdateDirtyFields(updateDirtyFields);
doc = new Document(getArtifactsDir() + "Filed.UpdateDirtyFields.docx", options);
Assert.assertEquals("John & Jane Doe", doc.getBuiltInDocumentProperties().getAuthor());
field = (FieldAuthor) doc.getRange().getFields().get(0);
// Updating dirty fields like this automatically set their "IsDirty" flag to false.
if (updateDirtyFields) {
Assert.assertEquals("John & Jane Doe", field.getResult());
Assert.assertFalse(field.isDirty());
} else {
Assert.assertEquals("John Doe", field.getResult());
Assert.assertTrue(field.isDirty());
}
Returns: boolean - Whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
isDirty(boolean value)
public void isDirty(boolean value)
Sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
Examples:
Shows how to use special property for updating field result.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Give the document's built-in "Author" property value, and then display it with a field.
doc.getBuiltInDocumentProperties().setAuthor("John Doe");
FieldAuthor field = (FieldAuthor) builder.insertField(FieldType.FIELD_AUTHOR, true);
Assert.assertFalse(field.isDirty());
Assert.assertEquals("John Doe", field.getResult());
// Update the property. The field still displays the old value.
doc.getBuiltInDocumentProperties().setAuthor("John & Jane Doe");
Assert.assertEquals("John Doe", field.getResult());
// Since the field's value is out of date, we can mark it as "dirty".
// This value will stay out of date until we update the field manually with the Field.Update() method.
field.isDirty(true);
// If we save without calling an update method,
// the field will keep displaying the out of date value in the output document.
doc.save(getArtifactsDir() + "Filed.UpdateDirtyFields.docx");
// The LoadOptions object has an option to update all fields
// marked as "dirty" when loading the document.
LoadOptions options = new LoadOptions();
options.setUpdateDirtyFields(updateDirtyFields);
doc = new Document(getArtifactsDir() + "Filed.UpdateDirtyFields.docx", options);
Assert.assertEquals("John & Jane Doe", doc.getBuiltInDocumentProperties().getAuthor());
field = (FieldAuthor) doc.getRange().getFields().get(0);
// Updating dirty fields like this automatically set their "IsDirty" flag to false.
if (updateDirtyFields) {
Assert.assertEquals("John & Jane Doe", field.getResult());
Assert.assertFalse(field.isDirty());
} else {
Assert.assertEquals("John Doe", field.getResult());
Assert.assertTrue(field.isDirty());
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether the current result of the field is no longer correct (stale) due to other modifications made to the document. |
isLocked()
public boolean isLocked()
Gets whether the field is locked (should not recalculate its result).
Examples:
Shows how to work with a FieldStart node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDate field = (FieldDate) builder.insertField(FieldType.FIELD_DATE, true);
field.getFormat().setDateTimeFormat("dddd, MMMM dd, yyyy");
field.update();
FieldChar fieldStart = field.getStart();
Assert.assertEquals(FieldType.FIELD_DATE, fieldStart.getFieldType());
Assert.assertEquals(false, fieldStart.isDirty());
Assert.assertEquals(false, fieldStart.isLocked());
// Retrieve the facade object which represents the field in the document.
field = (FieldDate) fieldStart.getField();
Assert.assertEquals(false, field.isLocked());
Assert.assertEquals(" DATE \\@ \"dddd, MMMM dd, yyyy\"", field.getFieldCode());
// Update the field to show the current date.
field.update();
Returns: boolean - Whether the field is locked (should not recalculate its result).
isLocked(boolean value)
public void isLocked(boolean value)
Sets whether the field is locked (should not recalculate its result).
Examples:
Shows how to work with a FieldStart node.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldDate field = (FieldDate) builder.insertField(FieldType.FIELD_DATE, true);
field.getFormat().setDateTimeFormat("dddd, MMMM dd, yyyy");
field.update();
FieldChar fieldStart = field.getStart();
Assert.assertEquals(FieldType.FIELD_DATE, fieldStart.getFieldType());
Assert.assertEquals(false, fieldStart.isDirty());
Assert.assertEquals(false, fieldStart.isLocked());
// Retrieve the facade object which represents the field in the document.
field = (FieldDate) fieldStart.getField();
Assert.assertEquals(false, field.isLocked());
Assert.assertEquals(" DATE \\@ \"dddd, MMMM dd, yyyy\"", field.getFieldCode());
// Update the field to show the current date.
field.update();
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether the field is locked (should not recalculate its result). |
remove()
public Node remove()
Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns null .
Examples:
Shows how to remove fields from a field collection.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder.insertField(" TIME ");
builder.insertField(" REVNUM ");
builder.insertField(" AUTHOR \"John Doe\" ");
builder.insertField(" SUBJECT \"My Subject\" ");
builder.insertField(" QUOTE \"Hello world!\" ");
doc.updateFields();
FieldCollection fields = doc.getRange().getFields();
Assert.assertEquals(6, fields.getCount());
// Below are four ways of removing fields from a field collection.
// 1 - Get a field to remove itself:
fields.get(0).remove();
Assert.assertEquals(5, fields.getCount());
// 2 - Get the collection to remove a field that we pass to its removal method:
Field lastField = fields.get(3);
fields.remove(lastField);
Assert.assertEquals(4, fields.getCount());
// 3 - Remove a field from a collection at an index:
fields.removeAt(2);
Assert.assertEquals(3, fields.getCount());
// 4 - Remove all the fields from the collection at once:
fields.clear();
Assert.assertEquals(0, fields.getCount());
Shows how to process PRIVATE fields.
public void fieldPrivate() throws Exception {
// Open a Corel WordPerfect document which we have converted to .docx format.
Document doc = new Document(getMyDir() + "Field sample - PRIVATE.docx");
// WordPerfect 5.x/6.x documents like the one we have loaded may contain PRIVATE fields.
// Microsoft Word preserves PRIVATE fields during load/save operations,
// but provides no functionality for them.
FieldPrivate field = (FieldPrivate) doc.getRange().getFields().get(0);
Assert.assertEquals(" PRIVATE \"My value\" ", field.getFieldCode());
Assert.assertEquals(FieldType.FIELD_PRIVATE, field.getType());
// We can also insert PRIVATE fields using a document builder.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertField(FieldType.FIELD_PRIVATE, true);
// These fields are not a viable way of protecting sensitive information.
// Unless backward compatibility with older versions of WordPerfect is essential,
// we can safely remove these fields. We can do this using a DocumentVisiitor implementation.
Assert.assertEquals(2, doc.getRange().getFields().getCount());
FieldPrivateRemover remover = new FieldPrivateRemover();
doc.accept(remover);
Assert.assertEquals(remover.getFieldsRemovedCount(), 2);
Assert.assertEquals(doc.getRange().getFields().getCount(), 0);
}
///
/// Removes all encountered PRIVATE fields.
///
public static class FieldPrivateRemover extends DocumentVisitor {
public FieldPrivateRemover() {
mFieldsRemovedCount = 0;
}
public int getFieldsRemovedCount() {
return mFieldsRemovedCount;
}
///
/// Called when a FieldEnd node is encountered in the document.
/// If the node belongs to a PRIVATE field, the entire field is removed.
///
public int visitFieldEnd(final FieldEnd fieldEnd) throws Exception {
if (fieldEnd.getFieldType() == FieldType.FIELD_PRIVATE) {
fieldEnd.getField().remove();
mFieldsRemovedCount++;
}
return VisitorAction.CONTINUE;
}
private int mFieldsRemovedCount;
}
Returns: Node
setBookmarkName(String value)
public void setBookmarkName(String value)
Sets the name of the bookmark that marks the portion of the document used to build the table.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the bookmark that marks the portion of the document used to build the table. |
setCaptionlessTableOfFiguresLabel(String value)
public void setCaptionlessTableOfFiguresLabel(String value)
Sets the name of the sequence identifier used when building a table of figures that does not include caption’s label and number.
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the sequence identifier used when building a table of figures that does not include caption’s label and number. |
setCustomStyles(String value)
public void setCustomStyles(String value)
Sets a list of styles other than the built-in heading styles to include in the table of contents.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A list of styles other than the built-in heading styles to include in the table of contents. |
setEntryIdentifier(String value)
public void setEntryIdentifier(String value)
Sets a string that should match type identifiers of TC fields being included.
Examples:
Shows how to insert a TOC field, and filter which TC fields end up as entries.
public void fieldTocEntryIdentifier() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a TOC field, which will compile all TC fields into a table of contents.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Configure the field only to pick up TC entries of the "A" type, and an entry-level between 1 and 3.
fieldToc.setEntryIdentifier("A");
fieldToc.setEntryLevelRange("1-3");
Assert.assertEquals(" TOC \\f A \\l 1-3", fieldToc.getFieldCode());
// These two entries will appear in the table.
builder.insertBreak(BreakType.PAGE_BREAK);
insertTocEntry(builder, "TC field 1", "A", "1");
insertTocEntry(builder, "TC field 2", "A", "2");
Assert.assertEquals(" TC \"TC field 1\" \\n \\f A \\l 1", doc.getRange().getFields().get(1).getFieldCode());
// This entry will be omitted from the table because it has a different type from "A".
insertTocEntry(builder, "TC field 3", "B", "1");
// This entry will be omitted from the table because it has an entry-level outside of the 1-3 range.
insertTocEntry(builder, "TC field 4", "A", "5");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TC.docx");
}
///
/// Use a document builder to insert a TC field.
///
public void insertTocEntry(final DocumentBuilder builder, final String text, final String typeIdentifier, final String entryLevel) throws Exception {
FieldTC fieldTc = (FieldTC) builder.insertField(FieldType.FIELD_TOC_ENTRY, true);
fieldTc.setOmitPageNumber(true);
fieldTc.setText(text);
fieldTc.setTypeIdentifier(typeIdentifier);
fieldTc.setEntryLevel(entryLevel);
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A string that should match type identifiers of TC fields being included. |
setEntryLevelRange(String value)
public void setEntryLevelRange(String value)
Sets a range of levels of the table of contents entries to be included.
Examples:
Shows how to insert a TOC field, and filter which TC fields end up as entries.
public void fieldTocEntryIdentifier() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a TOC field, which will compile all TC fields into a table of contents.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Configure the field only to pick up TC entries of the "A" type, and an entry-level between 1 and 3.
fieldToc.setEntryIdentifier("A");
fieldToc.setEntryLevelRange("1-3");
Assert.assertEquals(" TOC \\f A \\l 1-3", fieldToc.getFieldCode());
// These two entries will appear in the table.
builder.insertBreak(BreakType.PAGE_BREAK);
insertTocEntry(builder, "TC field 1", "A", "1");
insertTocEntry(builder, "TC field 2", "A", "2");
Assert.assertEquals(" TC \"TC field 1\" \\n \\f A \\l 1", doc.getRange().getFields().get(1).getFieldCode());
// This entry will be omitted from the table because it has a different type from "A".
insertTocEntry(builder, "TC field 3", "B", "1");
// This entry will be omitted from the table because it has an entry-level outside of the 1-3 range.
insertTocEntry(builder, "TC field 4", "A", "5");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TC.docx");
}
///
/// Use a document builder to insert a TC field.
///
public void insertTocEntry(final DocumentBuilder builder, final String text, final String typeIdentifier, final String entryLevel) throws Exception {
FieldTC fieldTc = (FieldTC) builder.insertField(FieldType.FIELD_TOC_ENTRY, true);
fieldTc.setOmitPageNumber(true);
fieldTc.setText(text);
fieldTc.setTypeIdentifier(typeIdentifier);
fieldTc.setEntryLevel(entryLevel);
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A range of levels of the table of contents entries to be included. |
setEntrySeparator(String value)
public void setEntrySeparator(String value)
Sets a sequence of characters that separate an entry and its page number.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A sequence of characters that separate an entry and its page number. |
setHeadingLevelRange(String value)
public void setHeadingLevelRange(String value)
Sets a range of heading levels to include.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A range of heading levels to include. |
setHideInWebLayout(boolean value)
public void setHideInWebLayout(boolean value)
Sets whether to hide tab leader and page numbers in Web layout view.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether to hide tab leader and page numbers in Web layout view. |
setInsertHyperlinks(boolean value)
public void setInsertHyperlinks(boolean value)
Sets whether to make the table of contents entries hyperlinks.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether to make the table of contents entries hyperlinks. |
setLocaleId(int value)
public void setLocaleId(int value)
Sets the LCID of the field.
Examples:
Shows how to insert a field and work with its locale.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a DATE field, and then print the date it will display.
// Your thread's current culture determines the formatting of the date.
Field field = builder.insertField("DATE");
System.out.println(MessageFormat.format("Today''s date, as displayed in the \"{0}\" culture: {1}", Locale.getDefault().getDisplayLanguage(), field.getResult()));
Assert.assertEquals(1033, field.getLocaleId());
// Changing the culture of our thread will impact the result of the DATE field.
// Another way to get the DATE field to display a date in a different culture is to use its LocaleId property.
// This way allows us to avoid changing the thread's culture to get this effect.
doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);
CultureInfo de = new CultureInfo("de-DE");
field.setLocaleId(1031);
field.update();
System.out.println(MessageFormat.format("Today''s date, as displayed according to the \"{0}\" culture: {1}", Locale.forLanguageTag(LocaleUtil.getLocaleFromLCID(field.getLocaleId())).getDisplayLanguage(), field.getResult()));
Parameters:
Parameter | Type | Description |
---|---|---|
value | int | The LCID of the field. |
setPageNumberOmittingLevelRange(String value)
public void setPageNumberOmittingLevelRange(String value)
Sets a range of levels of the table of contents entries from which to omits page numbers.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | A range of levels of the table of contents entries from which to omits page numbers. |
setPrefixedSequenceIdentifier(String value)
public void setPrefixedSequenceIdentifier(String value)
Sets the identifier of a sequence for which a prefix should be added to the entry’s page number.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The identifier of a sequence for which a prefix should be added to the entry’s page number. |
setPreserveLineBreaks(boolean value)
public void setPreserveLineBreaks(boolean value)
Sets whether to preserve newline characters within table entries.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether to preserve newline characters within table entries. |
setPreserveTabs(boolean value)
public void setPreserveTabs(boolean value)
Sets whether to preserve tab entries within table entries.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether to preserve tab entries within table entries. |
setResult(String value)
public void setResult(String value)
Sets text that is between the field separator and field end.
Examples:
Shows how to insert a field into a document using a field code.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Field dateField = builder.insertField("DATE \\* MERGEFORMAT");
Assert.assertEquals(FieldType.FIELD_DATE, dateField.getType());
Assert.assertEquals("DATE \\* MERGEFORMAT", dateField.getFieldCode());
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | Text that is between the field separator and field end. |
setSequenceSeparator(String value)
public void setSequenceSeparator(String value)
Sets the character sequence that is used to separate sequence numbers and page numbers.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The character sequence that is used to separate sequence numbers and page numbers. |
setTableOfFiguresLabel(String value)
public void setTableOfFiguresLabel(String value)
Sets the name of the sequence identifier used when building a table of figures.
Examples:
Shows how to populate a TOC field with entries using SEQ fields.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// A TOC field can create an entry in its table of contents for each SEQ field found in the document.
// Each entry contains the paragraph that includes the SEQ field and the page's number that the field appears on.
FieldToc fieldToc = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// SEQ fields display a count that increments at each SEQ field.
// These fields also maintain separate counts for each unique named sequence
// identified by the SEQ field's "SequenceIdentifier" property.
// Use the "TableOfFiguresLabel" property to name a main sequence for the TOC.
// Now, this TOC will only create entries out of SEQ fields with their "SequenceIdentifier" set to "MySequence".
fieldToc.setTableOfFiguresLabel("MySequence");
// We can name another SEQ field sequence in the "PrefixedSequenceIdentifier" property.
// SEQ fields from this prefix sequence will not create TOC entries.
// Every TOC entry created from a main sequence SEQ field will now also display the count that
// the prefix sequence is currently on at the primary sequence SEQ field that made the entry.
fieldToc.setPrefixedSequenceIdentifier("PrefixSequence");
// Each TOC entry will display the prefix sequence count immediately to the left
// of the page number that the main sequence SEQ field appears on.
// We can specify a custom separator that will appear between these two numbers.
fieldToc.setSequenceSeparator(">");
Assert.assertEquals(" TOC \\c MySequence \\s PrefixSequence \\d >", fieldToc.getFieldCode());
builder.insertBreak(BreakType.PAGE_BREAK);
// There are two ways of using SEQ fields to populate this TOC.
// 1 - Inserting a SEQ field that belongs to the TOC's prefix sequence:
// This field will increment the SEQ sequence count for the "PrefixSequence" by 1.
// Since this field does not belong to the main sequence identified
// by the "TableOfFiguresLabel" property of the TOC, it will not appear as an entry.
FieldSeq fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
Assert.assertEquals(" SEQ PrefixSequence", fieldSeq.getFieldCode());
// 2 - Inserting a SEQ field that belongs to the TOC's main sequence:
// This SEQ field will create an entry in the TOC.
// The TOC entry will contain the paragraph that the SEQ field is in and the number of the page that it appears on.
// This entry will also display the count that the prefix sequence is currently at,
// separated from the page number by the value in the TOC's SeqenceSeparator property.
// The "PrefixSequence" count is at 1, this main sequence SEQ field is on page 2,
// and the separator is ">", so entry will display "1>2".
builder.write("First TOC entry, MySequence #");
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("MySequence");
Assert.assertEquals(" SEQ MySequence", fieldSeq.getFieldCode());
// Insert a page, advance the prefix sequence by 2, and insert a SEQ field to create a TOC entry afterwards.
// The prefix sequence is now at 2, and the main sequence SEQ field is on page 3,
// so the TOC entry will display "2>3" at its page count.
builder.insertBreak(BreakType.PAGE_BREAK);
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
fieldSeq.setSequenceIdentifier("PrefixSequence");
builder.insertParagraph();
fieldSeq = (FieldSeq) builder.insertField(FieldType.FIELD_SEQUENCE, true);
builder.write("Second TOC entry, MySequence #");
fieldSeq.setSequenceIdentifier("MySequence");
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.SEQ.docx");
Parameters:
Parameter | Type | Description |
---|---|---|
value | java.lang.String | The name of the sequence identifier used when building a table of figures. |
setUseParagraphOutlineLevel(boolean value)
public void setUseParagraphOutlineLevel(boolean value)
Sets whether to use the applied paragraph outline level.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Parameters:
Parameter | Type | Description |
---|---|---|
value | boolean | Whether to use the applied paragraph outline level. |
unlink()
public boolean unlink()
Performs the field unlink.
Remarks:
Replaces the field with its most recent result.
Some fields, such as XE (Index Entry) fields and SEQ (Sequence) fields, cannot be unlinked.
Examples:
Shows how to unlink a field.
Document doc = new Document(getMyDir() + "Linked fields.docx");
doc.getRange().getFields().get(1).unlink();
Returns: boolean - true if the field has been unlinked, otherwise false .
update()
public void update()
Performs the field update. Throws if the field is being updated already.
Examples:
Shows how to insert a field into a document using FieldType.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert two fields while passing a flag which determines whether to update them as the builder inserts them.
// In some cases, updating fields could be computationally expensive, and it may be a good idea to defer the update.
doc.getBuiltInDocumentProperties().setAuthor("John Doe");
builder.write("This document was written by ");
builder.insertField(FieldType.FIELD_AUTHOR, updateInsertedFieldsImmediately);
builder.insertParagraph();
builder.write("\nThis is page ");
builder.insertField(FieldType.FIELD_PAGE, updateInsertedFieldsImmediately);
Assert.assertEquals(" AUTHOR ", doc.getRange().getFields().get(0).getFieldCode());
Assert.assertEquals(" PAGE ", doc.getRange().getFields().get(1).getFieldCode());
if (updateInsertedFieldsImmediately) {
Assert.assertEquals("John Doe", doc.getRange().getFields().get(0).getResult());
Assert.assertEquals("1", doc.getRange().getFields().get(1).getResult());
} else {
Assert.assertEquals("", doc.getRange().getFields().get(0).getResult());
Assert.assertEquals("", doc.getRange().getFields().get(1).getResult());
// We will need to update these fields using the update methods manually.
doc.getRange().getFields().get(0).update();
Assert.assertEquals("John Doe", doc.getRange().getFields().get(0).getResult());
doc.updateFields();
Assert.assertEquals("1", doc.getRange().getFields().get(1).getResult());
}
Shows how to format field results.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a field that displays a result with no format applied.
Field field = builder.insertField("= 2 + 3");
Assert.assertEquals("= 2 + 3", field.getFieldCode());
Assert.assertEquals("5", field.getResult());
// We can apply a format to a field's result using the field's properties.
// Below are three types of formats that we can apply to a field's result.
// 1 - Numeric format:
FieldFormat format = field.getFormat();
format.setNumericFormat("$###.00");
field.update();
Assert.assertEquals("= 2 + 3 \\# $###.00", field.getFieldCode());
Assert.assertEquals("$ 5.00", field.getResult());
// 2 - Date/time format:
field = builder.insertField("DATE");
format = field.getFormat();
format.setDateTimeFormat("dddd, MMMM dd, yyyy");
field.update();
Assert.assertEquals("DATE \\@ \"dddd, MMMM dd, yyyy\"", field.getFieldCode());
System.out.println("Today's date, in {format.DateTimeFormat} format:\n\t{field.Result}");
// 3 - General format:
field = builder.insertField("= 25 + 33");
format = field.getFormat();
format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().add(GeneralFormat.UPPER);
field.update();
int index = 0;
Iterator generalFormatEnumerator = format.getGeneralFormats().iterator();
while (generalFormatEnumerator.hasNext()) {
int value = generalFormatEnumerator.next();
System.out.println(MessageFormat.format("General format index {0}: {1}", index++, value));
}
Assert.assertEquals("= 25 + 33 \\* roman \\* Upper", field.getFieldCode());
Assert.assertEquals("LVIII", field.getResult());
Assert.assertEquals(2, format.getGeneralFormats().getCount());
Assert.assertEquals(GeneralFormat.LOWERCASE_ROMAN, format.getGeneralFormats().get(0));
// We can remove our formats to revert the field's result to its original form.
format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().removeAt(0);
Assert.assertEquals(0, format.getGeneralFormats().getCount());
field.update();
Assert.assertEquals("= 25 + 33 ", field.getFieldCode());
Assert.assertEquals("58", field.getResult());
Assert.assertEquals(0, format.getGeneralFormats().getCount());
update(boolean ignoreMergeFormat)
public void update(boolean ignoreMergeFormat)
Performs a field update. Throws if the field is being updated already.
Examples:
Shows how to preserve or discard INCLUDEPICTURE fields when loading a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldIncludePicture includePicture = (FieldIncludePicture) builder.insertField(FieldType.FIELD_INCLUDE_PICTURE, true);
includePicture.setSourceFullName(getImageDir() + "Transparent background logo.png");
includePicture.update(true);
try (ByteArrayOutputStream docStream = new ByteArrayOutputStream()) {
doc.save(docStream, new OoxmlSaveOptions(SaveFormat.DOCX));
// We can set a flag in a LoadOptions object to decide whether to convert all INCLUDEPICTURE fields
// into image shapes when loading a document that contains them.
LoadOptions loadOptions = new LoadOptions();
{
loadOptions.setPreserveIncludePictureField(preserveIncludePictureField);
}
doc = new Document(new ByteArrayInputStream(docStream.toByteArray()), loadOptions);
FieldCollection fieldCollection = doc.getRange().getFields();
if (preserveIncludePictureField) {
Assert.assertTrue(IterableUtils.matchesAny(fieldCollection, f -> f.getType() == FieldType.FIELD_INCLUDE_PICTURE));
doc.updateFields();
doc.save(getArtifactsDir() + "Field.PreserveIncludePicture.docx");
} else {
Assert.assertFalse(IterableUtils.matchesAny(fieldCollection, f -> f.getType() == FieldType.FIELD_INCLUDE_PICTURE));
}
}
Parameters:
Parameter | Type | Description |
---|---|---|
ignoreMergeFormat | boolean | If true then direct field result formatting is abandoned, regardless of the MERGEFORMAT switch, otherwise normal update is performed. |
updatePageNumbers()
public boolean updatePageNumbers()
Updates the page numbers for items in this table of contents.
Examples:
Shows how to insert a TOC, and populate it with entries based on heading styles.
public void fieldToc() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("MyBookmark");
// Insert a TOC field, which will compile all headings into a table of contents.
// For each heading, this field will create a line with the text in that heading style to the left,
// and the page the heading appears on to the right.
FieldToc field = (FieldToc) builder.insertField(FieldType.FIELD_TOC, true);
// Use the BookmarkName property to only list headings
// that appear within the bounds of a bookmark with the "MyBookmark" name.
field.setBookmarkName("MyBookmark");
// Text with a built-in heading style, such as "Heading 1", applied to it will count as a heading.
// We can name additional styles to be picked up as headings by the TOC in this property and their TOC levels.
field.setCustomStyles("Quote; 6; Intense Quote; 7");
// By default, Styles/TOC levels are separated in the CustomStyles property by a comma,
// but we can set a custom delimiter in this property.
doc.getFieldOptions().setCustomTocStyleSeparator(";");
// Configure the field to exclude any headings that have TOC levels outside of this range.
field.setHeadingLevelRange("1-3");
// The TOC will not display the page numbers of headings whose TOC levels are within this range.
field.setPageNumberOmittingLevelRange("2-5");
// Set a custom string that will separate every heading from its page number.
field.setEntrySeparator("-");
field.setInsertHyperlinks(true);
field.setHideInWebLayout(false);
field.setPreserveLineBreaks(true);
field.setPreserveTabs(true);
field.setUseParagraphOutlineLevel(false);
insertNewPageWithHeading(builder, "First entry", "Heading 1");
builder.writeln("Paragraph text.");
insertNewPageWithHeading(builder, "Second entry", "Heading 1");
insertNewPageWithHeading(builder, "Third entry", "Quote");
insertNewPageWithHeading(builder, "Fourth entry", "Intense Quote");
// These two headings will have the page numbers omitted because they are within the "2-5" range.
insertNewPageWithHeading(builder, "Fifth entry", "Heading 2");
insertNewPageWithHeading(builder, "Sixth entry", "Heading 3");
// This entry does not appear because "Heading 4" is outside of the "1-3" range that we have set earlier.
insertNewPageWithHeading(builder, "Seventh entry", "Heading 4");
builder.endBookmark("MyBookmark");
builder.writeln("Paragraph text.");
// This entry does not appear because it is outside the bookmark specified by the TOC.
insertNewPageWithHeading(builder, "Eighth entry", "Heading 1");
Assert.assertEquals(" TOC \\b MyBookmark \\t \"Quote; 6; Intense Quote; 7\" \\o 1-3 \\n 2-5 \\p - \\h \\x \\w", field.getFieldCode());
field.updatePageNumbers();
doc.updateFields();
doc.save(getArtifactsDir() + "Field.TOC.docx");
}
///
/// Start a new page and insert a paragraph of a specified style.
///
public void insertNewPageWithHeading(final DocumentBuilder builder, final String captionText, final String styleName) {
builder.insertBreak(BreakType.PAGE_BREAK);
String originalStyle = builder.getParagraphFormat().getStyleName();
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(styleName));
builder.writeln(captionText);
builder.getParagraphFormat().setStyle(builder.getDocument().getStyles().get(originalStyle));
}
Returns: boolean - true if the operation is successful. If any of the related TOC bookmarks was removed, false will be returned.