Class TableOfContent
Represent a table of contents in the Word document.
Inheritance
Inherited Members
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.UWP.dll
Syntax
public class TableOfContent : ParagraphItem, IXDLSSerializable, IWidget, IParagraphItem, IEntity, IOfficeRun
Examples
The following example illustrates how to add table of contents to a Word document.
//Create a new Word document
WordDocument document = new WordDocument();
//Add the section into the Word document
IWSection section = document.AddSection();
string paraText = "Lorem ipsum dolor sit amet, lacus amet amet ultricies. Quisque mi venenatis morbi libero, orci dis, mi ut et class porta, massa ligula magna enim, aliquam orci vestibulum Turpis facilisis vitae consequat, cum a a,turpis dui consequat massa in dolor per, felis non amet.Auctor eleifend in omnis elit vestibulum, donec non elementum tellus est mauris, id aliquam, at lacus, arcu pretium proin lacus dolor et. Eu tortor, vel ultrices amet dignissim mauris vehicula";
//Add the paragraph into the created section
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries
paragraph.AppendTOC(1, 3);
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("First Chapter");
//Set a build in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("Second Chapter");
//Set a build in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading2);
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text into the headings
paragraph.AppendText("Third Chapter");
//Set a build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading3);
//Add the text into the paragraph.
section.AddParagraph().AppendText(paraText);
//Update the table of contents
document.UpdateTableOfContents();
//Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx);
document.Close();
'Create a new Word document
Dim document As New WordDocument()
'Add the section into the Word document
Dim section As IWSection = document.AddSection()
Dim paraText As String = "Lorem ipsum dolor sit amet, lacus amet amet ultricies. Quisque mi venenatis morbi libero, orci dis, mi ut et class porta, massa ligula magna enim, aliquam orci vestibulum Turpis facilisis vitae consequat, cum a a,turpis dui consequat massa in dolor per, felis non amet.Auctor eleifend in omnis elit vestibulum, donec non elementum tellus est mauris, id aliquam, at lacus, arcu pretium proin lacus dolor et. Eu tortor, vel ultrices amet dignissim mauris vehicula"
'Add the paragraph into the created section
Dim paragraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries
paragraph.AppendTOC(1, 3)
'Add the section into the Word document
section = document.AddSection()
'Add the paragraph into the created section
paragraph = section.AddParagraph()
'Add the text for the headings
paragraph.AppendText("First Chapter")
'Set a build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading1)
'Add the text into the paragraph.
section.AddParagraph().AppendText(paraText)
'Add the section into the Word document
section = document.AddSection()
'Add the paragraph into the created section
paragraph = section.AddParagraph()
'Add the text for the headings
paragraph.AppendText("Second Chapter")
'Set a build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading2)
'Add the text into the paragraph
section.AddParagraph().AppendText(paraText)
'Add the section into the Word document
section = document.AddSection()
'Add the paragraph into the created section
paragraph = section.AddParagraph()
'Add the text into the headings
paragraph.AppendText("Third Chapter")
'Set a build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading3)
'Add the text into the paragraph
section.AddParagraph().AppendText(paraText)
'Update the table of contents
document.UpdateTableOfContents()
'Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx)
document.Close()
Constructors
TableOfContent(IWordDocument)
Initializes a new instance of the TableOfContent class with the specified IWordDocument instance.
Declaration
public TableOfContent(IWordDocument doc)
Parameters
Type | Name | Description |
---|---|---|
IWordDocument | doc | The IWordDocument instance. |
TableOfContent(IWordDocument, String)
Initializes a new instance of the TableOfContent class with the specified IWordDocument instance and switches.
Declaration
public TableOfContent(IWordDocument doc, string switches)
Parameters
Type | Name | Description |
---|---|---|
IWordDocument | doc | The IWordDocument instance. |
System.String | switches | The string that specifies the formatting string of the TOC field. |
Properties
EntityType
Gets the type of the entity.
Declaration
public override EntityType EntityType { get; }
Property Value
Type | Description |
---|---|
EntityType | The EntityType of the current TableOfContent. |
Overrides
IncludeCaptionLabelsAndNumbers
Gets or sets a value indicating whether to include caption's labels and numbers while building a table of figures based on identifier TableOfFiguresLabel. The default value is true.
Declaration
public bool IncludeCaptionLabelsAndNumbers { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
|
Remarks
This property corresponds to the \a switch of the TOC field.
Examples
//Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a new section to the document.
IWSection section = document.AddSection();
//Add a paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
//Set the name of SEQ field identifier for table of figures.
tableOfContent.TableOfFiguresLabel = "Figure";
//Disable the flag, to exclude caption's label and number in TOC entries.
tableOfContent.IncludeCaptionLabelsAndNumbers = false;
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Add image to the paragraph.
FileStream imageStream = new FileStream(@"Image.png", FileMode.Open, FileAccess.ReadWrite);
IWPicture picture = paragraph.AppendPicture(imageStream);
//Add Image caption.
IWParagraph paragraph1 = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage);
//Add text to the paragraph.
paragraph1.AppendText(" Icon.");
//Update the fields in Word document.
document.UpdateDocumentFields();
//Update the table of contents.
document.UpdateTableOfContents();
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
}
'Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add a new section to the document.
Dim section As IWSection = document.AddSection()
'Add a paragraph to the section.
Dim paragraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
Dim tableOfContent As TableOfContent = paragraph.AppendTOC(1, 3)
'Set the name of SEQ field identifier for table of figures.
tableOfContent.TableOfFiguresLabel = "Figure"
'Disable the flag, to exclude caption's label and number in TOC entries.
tableOfContent.IncludeCaptionLabelsAndNumbers = False
'Add a paragraph to the section.
paragraph = section.AddParagraph()
'Add image to the paragraph.
Dim imageStream As FileStream = New FileStream("Image.png", FileMode.Open, FileAccess.ReadWrite)
Dim picture As IWPicture = paragraph.AppendPicture(imageStream)
'Add Image caption.
Dim paragraph1 As IWParagraph = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage)
'Add text to the paragraph.
paragraph1.AppendText(" Icon.")
'Update the fields in Word document.
document.UpdateDocumentFields()
'Update the table of contents.
document.UpdateTableOfContents()
'Save the document.
document.Save("Result.docx")
document.Close()
IncludeNewLineCharacters
Gets or sets a value indicating whether to include newline characters in TOC entries. Default value is false.
Declaration
public bool IncludeNewLineCharacters { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if it includes newline characters in TOC entries; otherwise, false. |
Examples
//Create a Word document.
WordDocument document = new WordDocument();
//Adds the section into the Word document.
IWSection section = document.AddSection();
//Adds the paragraph into the created section.
IWParagraph tocParagraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
TableOfContent tableOfContent = tocParagraph.AppendTOC(1, 3);
//Enables the property IncludeNewLineCharacters to preserve newline characters in the TableOfContent.
tableOfContent.IncludeNewLineCharacters = true;
//Adds the paragraph into the section.
WParagraph paragraph = section.AddParagraph() as WParagraph;
//Adds the text and breaks the headings.
paragraph.AppendText("First ");
paragraph.AppendBreak(BreakType.LineBreak);
paragraph.AppendText("Chapter");
//Sets a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Adds the text into the new paragraph of the section.
section.AddParagraph().AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.");
//Updated the table of content.
document.UpdateTableOfContents();
//Saves the document.
document.Save("Result.docx");
document.Close();
'Create a Word document.
Dim document As WordDocument = New WordDocument()
'Add the section into the Word document.
Dim section As IWSection = document.AddSection()
'Add the paragraph into the created section.
Dim tocParagraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
Dim tableOfContent As TableOfContent = tocParagraph.AppendTOC(1, 3)
'Enable the property IncludeNewLineCharacters to preserve newline characters in TableOfContent.
tableOfContent.IncludeNewLineCharacters = True
'Add the paragraph into the section.
Dim paragraph As WParagraph = TryCast(section.AddParagraph(), WParagraph)
'Add the text and break the headings.
paragraph.AppendText("First ")
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendText("Chapter")
'Set a built-in heading style.
paragraph.ApplyStyle(BuiltinStyle.Heading1)
'Add the text into the new paragraph of the section.
section.AddParagraph().AppendText("AdventureWorks Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.")
'Updated the table of content.
document.UpdateTableOfContents()
'Save the document.
document.Save("Result.docx")
document.Close()
IncludePageNumbers
Gets or sets a value indicating whether to show page numbers in table of contents. The default value is true.
Declaration
public bool IncludePageNumbers { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if to includes page numbers; otherwise, false. |
LowerHeadingLevel
Gets or sets the starting heading level of the table of contents. The default value is 1.
Declaration
public int LowerHeadingLevel { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 | The integer specifying the starting heading level. |
RightAlignPageNumbers
Gets or sets a value indicating whether to show page numbers as right aligned. The default value is true.
Declaration
public bool RightAlignPageNumbers { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if to right align page numbers; otherwise, false. |
TableID
Gets or sets the table ID.
Declaration
public string TableID { get; set; }
Property Value
Type | Description |
---|---|
System.String | The table ID. |
TableOfFiguresLabel
Gets or sets the name of the sequence identifier to be used when building a table of figures. The default value is null.
Declaration
public string TableOfFiguresLabel { get; set; }
Property Value
Type | Description |
---|---|
System.String | The System.String specifying the identifier. |
Remarks
1. This property corresponds to the \c switch of the TOC field.
2. Apart from caption fields of tables, figures, and shapes, it also considers any valid identifier used by a SEQ field.
Examples
//Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a new section to the document.
IWSection section = document.AddSection();
//Add a paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
//Set the name of SEQ field identifier for table of figures.
tableOfContent.TableOfFiguresLabel = "Figure";
//Add a paragraph to the section.
paragraph = section.AddParagraph();
//Add image to the paragraph.
FileStream imageStream = new FileStream(@"Image.png", FileMode.Open, FileAccess.ReadWrite);
IWPicture picture = paragraph.AppendPicture(imageStream);
//Add Image caption.
IWParagraph paragraph1 = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage);
//Add text to the paragraph.
paragraph1.AppendText(" Icon.");
//Update the fields in Word document.
document.UpdateDocumentFields();
//Update the table of contents.
document.UpdateTableOfContents();
//Saves the Word document to MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
}
'Create a new Word document.
Dim document As WordDocument = New WordDocument()
'Add a new section to the document.
Dim section As IWSection = document.AddSection()
'Add a paragraph to the section.
Dim paragraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries.
Dim tableOfContent As TableOfContent = paragraph.AppendTOC(1, 3)
'Set the name of SEQ field identifier for table of figures.
tableOfContent.TableOfFiguresLabel = "Figure"
'Add a paragraph to the section.
paragraph = section.AddParagraph()
'Add image to the paragraph.
Dim imageStream As FileStream = New FileStream("Image.png", FileMode.Open, FileAccess.ReadWrite)
Dim picture As IWPicture = paragraph.AppendPicture(imageStream)
'Add Image caption.
Dim paragraph1 As IWParagraph = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage)
'Add text to the paragraph.
paragraph1.AppendText(" Icon.")
'Update the fields in Word document.
document.UpdateDocumentFields()
'Update the table of contents.
document.UpdateTableOfContents()
'Save the document.
document.Save("Result.docx")
document.Close()
UpperHeadingLevel
Gets or sets the ending heading level of the table of contents. The default value is 3.
Declaration
public int UpperHeadingLevel { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 | The integer specifying the upper heading level. |
UseHeadingStyles
Gets or sets a value indicating whether to use default heading styles.
Declaration
public bool UseHeadingStyles { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if it uses heading styles; otherwise false. |
UseHyperlinks
Gets or sets a value indicating whether to use hyperlinks for the levels. The default value is true.
Declaration
public bool UseHyperlinks { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if it uses hyperlinks for levels; otherwise false. |
UseOutlineLevels
Gets or sets a value indicating whether to use outline levels. The default value is false.
Declaration
public bool UseOutlineLevels { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if it uses outline levels; otherwise false. |
UseTableEntryFields
Gets or sets a value indicating whether to use table entry fields. The default value is false.
Declaration
public bool UseTableEntryFields { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean | True if it uses table entry fields; otherwise, false. |
Methods
CloneImpl()
Creates a duplicate copy of the TableOfContent.
Declaration
protected override object CloneImpl()
Returns
Type | Description |
---|---|
System.Object | The reference of the newly created TableOfContent object. |
Overrides
GetTOCLevelStyle(Int32)
Returns the name of the style applied to the specified TOC level.
Declaration
public string GetTOCLevelStyle(int levelNumber)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | levelNumber | The level number of the table of contents. |
Returns
Type | Description |
---|---|
System.String | The name of the style. |
Examples
The following example illustrates how to get the name of the style applied to the TOC level.
//Load a Word document
WordDocument document = new WordDocument("Template.docx");
WTextBody textBody = document.Sections[0].Body;
WParagraph paragraph = textBody.Paragraphs[0];
//Get the TOC item from the paragraph
TableOfContent toc = paragraph.ChildEntities[0] as TableOfContent;
//Gets the style name for the specified level
string styleName = toc.GetTOCLevelStyle(1);
WParagraphStyle style = document.Styles.FindByName(styleName) as WParagraphStyle;
//Modify the character format of the style
style.CharacterFormat.HighlightColor = Color.LightGray;
//Save the Word document
document.Save("Sample.docx", FormatType.Docx);
//Close the document
document.Close();
'Load a Word document
Dim document As New WordDocument("Template.docx")
Dim textBody As WTextBody = document.Sections(0).Body
Dim paragraph As WParagraph = textBody.Paragraphs(0)
'Get the TOC item from the paragraph
Dim toc As TableOfContent = TryCast(paragraph.ChildEntities(0), TableOfContent)
'Gets the style name for the specified level
Dim styleName As String = toc.GetTOCLevelStyle(1)
Dim style As WParagraphStyle = TryCast(document.Styles.FindByName(styleName), WParagraphStyle)
'Modify the character format of the style
style.CharacterFormat.HighlightColor = Color.LightGray
'Save the Word document
document.Save("Sample.docx", FormatType.Docx)
'Close the document
document.Close()
GetTOCLevelStyles(Int32)
Returns the list of styles defined for the specified TOC level.
Declaration
public List<string> GetTOCLevelStyles(int levelNumber)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | levelNumber | The level number of the table of contents. |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.String> | The collection of styles defined for the specified TOC level. |
InitXDLSHolder()
Registers child objects in XDSL holder.
Declaration
protected override void InitXDLSHolder()
Overrides
SetTOCLevelStyle(Int32, String)
Sets the style for the TOC level.
Declaration
public void SetTOCLevelStyle(int levelNumber, string styleName)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | levelNumber | The level number of the table of contents. |
System.String | styleName | The name of the style to apply. |
Examples
The following code illustrates how to set the style for the TOC level.
//Create a new Word document
WordDocument document = new WordDocument();
//Create a new custom styles
Style style = (WParagraphStyle)document.AddParagraphStyle("Mystyle");
style.CharacterFormat.Bold = true;
style.CharacterFormat.FontName = "Verdana";
style.CharacterFormat.FontSize = 25;
//Add the section into the Word document
IWSection section = document.AddSection();
string paraText = "Lorem ipsum dolor sit amet, lacus amet amet ultricies. Quisque mi venenatis morbi libero, orci dis, mi ut et class porta, massa ligula magna enim, aliquam orci vestibulum Turpis facilisis vitae consequat, cum a a,turpis dui consequat massa in dolor per, felis non amet.Auctor eleifend in omnis elit vestibulum, donec non elementum tellus est mauris, id aliquam, at lacus, arcu pretium proin lacus dolor et. Eu tortor, vel ultrices amet dignissim mauris vehicula";
//Add the paragraph into the created section
IWParagraph paragraph = section.AddParagraph();
//Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determines the TOC entries
TableOfContent toc = paragraph.AppendTOC(1, 3);
toc.UseHeadingStyles = false;
//Set the TOC level style based on which the TOC should be created
toc.SetTOCLevelStyle(2, "Mystyle");
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("First Chapter");
//Set the build in heading style
paragraph.ApplyStyle("Mystyle");
//Add the text into the paragraph
section.AddParagraph().AppendText(paraText);
//Add the section into the Word document
section = document.AddSection();
//Add the paragraph into the created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("Second Chapter");
//Set the build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading1);
//Add the text to the paragraph
section.AddParagraph().AppendText(paraText);
//Add the section into Word document
section = document.AddSection();
//Add a paragraph to created section
paragraph = section.AddParagraph();
//Add the text for the headings
paragraph.AppendText("Third Chapter");
//Set the build in heading style
paragraph.ApplyStyle("Mystyle");
//Add the text to the paragraph
section.AddParagraph().AppendText(paraText);
//Update the table of contents
document.UpdateTableOfContents();
//Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx);
document.Close();
'Create a new Word document
Dim document As New WordDocument()
'Create a new custom styles
Dim style As Style = DirectCast(document.AddParagraphStyle("Mystyle"), WParagraphStyle)
style.CharacterFormat.Bold = True
style.CharacterFormat.FontName = "Verdana"
style.CharacterFormat.FontSize = 25
'Add the section into the Word document
Dim section As IWSection = document.AddSection()
Dim paraText As String = "Lorem ipsum dolor sit amet, lacus amet amet ultricies. Quisque mi venenatis morbi libero, orci dis, mi ut et class porta, massa ligula magna enim, aliquam orci vestibulum Turpis facilisis vitae consequat, cum a a,turpis dui consequat massa in dolor per, felis non amet.Auctor eleifend in omnis elit vestibulum, donec non elementum tellus est mauris, id aliquam, at lacus, arcu pretium proin lacus dolor et. Eu tortor, vel ultrices amet dignissim mauris vehicula"
'Add the paragraph into the created section
Dim paragraph As IWParagraph = section.AddParagraph()
'Append the TOC field with LowerHeadingLevel and UpperHeadingLevel to determine the TOC entries
Dim toc As TableOfContent = paragraph.AppendTOC(1, 3)
toc.UseHeadingStyles = False
'Set the TOC level style based on which the TOC should be created
toc.SetTOCLevelStyle(2, "Mystyle")
'Add the section into the Word document
section = document.AddSection()
'Add the paragraph into the created section
paragraph = section.AddParagraph()
'Add the text for the headings
paragraph.AppendText("First Chapter")
'Set the build in heading style
paragraph.ApplyStyle("Mystyle")
'Add the text into the paragraph
section.AddParagraph().AppendText(paraText)
'Add the section into the Word document
section = document.AddSection()
'Add the paragraph into the created section
paragraph = section.AddParagraph()
'Add the text for the headings
paragraph.AppendText("Second Chapter")
'Set the build in heading style
paragraph.ApplyStyle(BuiltinStyle.Heading1)
'Add the text to the paragraph
section.AddParagraph().AppendText(paraText)
'Add the section into Word document
section = document.AddSection()
'Add a paragraph to created section
paragraph = section.AddParagraph()
'Add the text for the headings
paragraph.AppendText("Third Chapter")
'Set the build in heading style
paragraph.ApplyStyle("Mystyle")
'Add the text to the paragraph
section.AddParagraph().AppendText(paraText)
'Update the table of contents
document.UpdateTableOfContents()
'Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx)
document.Close()
WriteXmlAttributes(IXDLSAttributeWriter)
Writes object data as xml attributes.
Declaration
protected override void WriteXmlAttributes(IXDLSAttributeWriter writer)
Parameters
Type | Name | Description |
---|---|---|
IXDLSAttributeWriter | writer | The IXDLSAttributeWriter object. |