Can you provide an example of coding for Table of Contents.

Technical support for Spire.Doc

Moderator: iceblue support

Can you provide an example of coding for Table of Contents.

Postby edward1 » Thu Aug 25, 2011 11:01 pm


I could do with a nice example of coding to create a "Table of contents".

Currently I use a VBA Macro which replaces a "{TOC}" placeholder with a "recording" of References/Table of contents/Automatic Table 1

I guess I really need the same format but with all fonts in calibri and "Contents" title in black calibri.

Any help greatly appreciated,

Posts: 3
Joined: Thu Aug 25, 2011 9:01 pm

Re: Can you provide an example of coding for Table of Conten

Postby » Fri Aug 26, 2011 7:42 am

Spire.Doc does support TOC, but it just adds a field of TOC into word document. The TOC items will not be generated. So it is required to update the filed manually when we open such a word document via MS-Word.
Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;

namespace TOC
    class Program
        static void Main(string[] args)
            //Open a blank word document as template
            Document document = new Document();

            //Get the first secition
            Section section = document.AddSection();

            //Create a new paragraph or get the first paragraph
            Paragraph paragraph = section.AddParagraph();

            //Append Text
            paragraph = section.AddParagraph();
            paragraph.AppendTOC(1, 4);
            paragraph = section.AddParagraph();

            foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
                paragraph = section.AddParagraph();
                //Append Text
                //Apply Style

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

Technical Support / Developer,
e-iceblue Support Team
User avatar
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 1 guest