public class FieldToc extends 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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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");
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getBookmarkName()
Gets the name of the bookmark that marks the portion of the document used to build the table.
|
java.lang.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.
|
java.lang.String |
getCustomStyles()
Gets a list of styles other than the built-in heading styles to include in the table of contents.
|
java.lang.String |
getEntryIdentifier()
Gets a string that should match type identifiers of TC fields being included.
|
java.lang.String |
getEntryLevelRange()
Gets a range of levels of the table of contents entries to be included.
|
java.lang.String |
getEntrySeparator()
Gets a sequence of characters that separate an entry and its page number.
|
java.lang.String |
getHeadingLevelRange()
Gets a range of heading levels to include.
|
boolean |
getHideInWebLayout()
Gets whether to hide tab leader and page numbers in Web layout view.
|
boolean |
getInsertHyperlinks()
Gets whether to make the table of contents entries hyperlinks.
|
java.lang.String |
getPageNumberOmittingLevelRange()
Gets a range of levels of the table of contents entries from which to omits page numbers.
|
java.lang.String |
getPrefixedSequenceIdentifier()
Gets the identifier of a sequence for which a prefix should be added to the entry's page number.
|
boolean |
getPreserveLineBreaks()
Gets whether to preserve newline characters within table entries.
|
boolean |
getPreserveTabs()
Gets whether to preserve tab entries within table entries.
|
java.lang.String |
getSequenceSeparator()
Gets the character sequence that is used to separate sequence numbers and page numbers.
|
int |
getSwitchType(java.lang.String switchName) |
java.lang.String |
getTableOfFiguresLabel()
Gets the name of the sequence identifier used when building a table of figures.
|
boolean |
getUseParagraphOutlineLevel()
Gets whether to use the applied paragraph outline level.
|
void |
setBookmarkName(java.lang.String value)
Sets the name of the bookmark that marks the portion of the document used to build the table.
|
void |
setCaptionlessTableOfFiguresLabel(java.lang.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.
|
void |
setCustomStyles(java.lang.String value)
Sets a list of styles other than the built-in heading styles to include in the table of contents.
|
void |
setEntryIdentifier(java.lang.String value)
Sets a string that should match type identifiers of TC fields being included.
|
void |
setEntryLevelRange(java.lang.String value)
Sets a range of levels of the table of contents entries to be included.
|
void |
setEntrySeparator(java.lang.String value)
Sets a sequence of characters that separate an entry and its page number.
|
void |
setHeadingLevelRange(java.lang.String value)
Sets a range of heading levels to include.
|
void |
setHideInWebLayout(boolean value)
Sets whether to hide tab leader and page numbers in Web layout view.
|
void |
setInsertHyperlinks(boolean value)
Sets whether to make the table of contents entries hyperlinks.
|
void |
setPageNumberOmittingLevelRange(java.lang.String value)
Sets a range of levels of the table of contents entries from which to omits page numbers.
|
void |
setPrefixedSequenceIdentifier(java.lang.String value)
Sets the identifier of a sequence for which a prefix should be added to the entry's page number.
|
void |
setPreserveLineBreaks(boolean value)
Sets whether to preserve newline characters within table entries.
|
void |
setPreserveTabs(boolean value)
Sets whether to preserve tab entries within table entries.
|
void |
setSequenceSeparator(java.lang.String value)
Sets the character sequence that is used to separate sequence numbers and page numbers.
|
void |
setTableOfFiguresLabel(java.lang.String value)
Sets the name of the sequence identifier used when building a table of figures.
|
void |
setUseParagraphOutlineLevel(boolean value)
Sets whether to use the applied paragraph outline level.
|
boolean |
updatePageNumbers()
Updates the page numbers for items in this table of contents.
|
getDisplayResult, getEnd, getFieldCode, getFieldCode, getFormat, getLocaleId, getResult, getSeparator, getStart, getType, isDirty, isDirty, isLocked, isLocked, needStoreOldResultNodes, remove, setLocaleId, setResult, unlink, update, updatepublic boolean updatePageNumbers()
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
true if the operation is successful. If any of the related TOC bookmarks was removed, false will be returned.java.lang.Exceptionpublic int getSwitchType(java.lang.String switchName)
public java.lang.String getBookmarkName()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setBookmarkName(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - The name of the bookmark that marks the portion of the document used to build the table.java.lang.Exceptionpublic java.lang.String getTableOfFiguresLabel()
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");
public void setTableOfFiguresLabel(java.lang.String value)
throws java.lang.Exception
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");
value - The name of the sequence identifier used when building a table of figures.java.lang.Exceptionpublic java.lang.String getCaptionlessTableOfFiguresLabel()
Examples:
Shows how to set the name of the sequence identifier.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldToc fieldToc = (FieldToc)builder.insertField(FieldType.FIELD_TOC, true);
fieldToc.setCaptionlessTableOfFiguresLabel("Test");
Assert.assertEquals(" TOC \\a Test", fieldToc.getFieldCode());
public void setCaptionlessTableOfFiguresLabel(java.lang.String value)
throws java.lang.Exception
Examples:
Shows how to set the name of the sequence identifier.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldToc fieldToc = (FieldToc)builder.insertField(FieldType.FIELD_TOC, true);
fieldToc.setCaptionlessTableOfFiguresLabel("Test");
Assert.assertEquals(" TOC \\a Test", fieldToc.getFieldCode());
value - The name of the sequence identifier used when building a table of figures that does not include caption's label and number.java.lang.Exceptionpublic java.lang.String getSequenceSeparator()
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");
public void setSequenceSeparator(java.lang.String value)
throws java.lang.Exception
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");
value - The character sequence that is used to separate sequence numbers and page numbers.java.lang.Exceptionpublic java.lang.String getEntryIdentifier()
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");
}
/// <summary>
/// Use a document builder to insert a TC field.
/// </summary>
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);
}
public void setEntryIdentifier(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Use a document builder to insert a TC field.
/// </summary>
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);
}
value - A string that should match type identifiers of TC fields being included.java.lang.Exceptionpublic boolean getInsertHyperlinks()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setInsertHyperlinks(boolean value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - Whether to make the table of contents entries hyperlinks.java.lang.Exceptionpublic java.lang.String getEntryLevelRange()
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");
}
/// <summary>
/// Use a document builder to insert a TC field.
/// </summary>
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);
}
public void setEntryLevelRange(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Use a document builder to insert a TC field.
/// </summary>
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);
}
value - A range of levels of the table of contents entries to be included.java.lang.Exceptionpublic java.lang.String getPageNumberOmittingLevelRange()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setPageNumberOmittingLevelRange(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - A range of levels of the table of contents entries from which to omits page numbers.java.lang.Exceptionpublic java.lang.String getHeadingLevelRange()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setHeadingLevelRange(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - A range of heading levels to include.java.lang.Exceptionpublic java.lang.String getEntrySeparator()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setEntrySeparator(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - A sequence of characters that separate an entry and its page number.java.lang.Exceptionpublic java.lang.String getPrefixedSequenceIdentifier()
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");
public void setPrefixedSequenceIdentifier(java.lang.String value)
throws java.lang.Exception
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");
value - The identifier of a sequence for which a prefix should be added to the entry's page number.java.lang.Exceptionpublic java.lang.String getCustomStyles()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setCustomStyles(java.lang.String value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - A list of styles other than the built-in heading styles to include in the table of contents.java.lang.Exceptionpublic boolean getUseParagraphOutlineLevel()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setUseParagraphOutlineLevel(boolean value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - Whether to use the applied paragraph outline level.java.lang.Exceptionpublic boolean getPreserveTabs()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setPreserveTabs(boolean value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - Whether to preserve tab entries within table entries.java.lang.Exceptionpublic boolean getPreserveLineBreaks()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setPreserveLineBreaks(boolean value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - Whether to preserve newline characters within table entries.java.lang.Exceptionpublic boolean getHideInWebLayout()
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
public void setHideInWebLayout(boolean value)
throws java.lang.Exception
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");
}
/// <summary>
/// Start a new page and insert a paragraph of a specified style.
/// </summary>
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));
}
value - Whether to hide tab leader and page numbers in Web layout view.java.lang.Exception