Hey,
I am inserting a TOC with these parameters: "\\o \"1-3\" \\h \\z \\u "
But I cannot find any information on how to format the toc.
Hey,
I am inserting a TOC with these parameters: "\\o \"1-3\" \\h \\z \\u "
But I cannot find any information on how to format the toc.
Hi Raphael,
Thasnk for your inquiry. Please use DocumentBuilder.InsertTableOfContents method to insert a TOC (table of contents) field into the document. If you insert a table of contents using this method and then open the file in Microsoft Word, you will not see the table of contents because the TOC field has not yet been updated. Please call Document.UpdateFields method to update the TOC field as shown in following code example.
// Use a blank document
Document doc = new Document();
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
// Start the actual document content on the second page.
builder.InsertBreak(BreakType.PageBreak);
// Build a document with complex structure by applying different heading styles thus creating TOC entries.
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");
// Call the method below to update the TOC.
doc.UpdateFields();
You can change the font formatting of TOC. Please check following code snippet. Hope this helps you.
Document doc = new Document(MyDir + "in.docx");
doc.Styles[StyleIdentifier.Toc1].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc2].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc3].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc4].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc5].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc6].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc7].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc8].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Toc9].Font.Color = Color.Red;