Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Aug 25, 2011 11:01 pm

Hi,

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,

Edward

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

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.AppendText("Content");
            paragraph = section.AddParagraph();
            paragraph.AppendTOC(1, 4);
            paragraph = section.AddParagraph();
            paragraph.AppendBreak(BreakType.PageBreak);

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

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start("Sample.doc");
        }
    }
}

Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc