Class TableOfContent
Represent a table of contents in the Word document.
Inheritance
Inherited Members
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.Base.dll
Syntax
public class TableOfContent : ParagraphItem, IXDLSSerializable, IWidget, IParagraphItem, IEntity, IOfficeRun, ILayoutInfo
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
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. |
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
CreateLayoutInfo()
Creates layout information.
Declaration
protected override void CreateLayoutInfo()
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. |