Insert table of contents

I just downloaded the trial version of Aspose.Word 1.7. I would like to generate a word document from a SQL Server database or import from existing Crystal report, and then generate the TOC (Table of Contents) including the page number based on the data within the document. Do you have any example to do this? I need this to be working before I can submit a proposal to purchase the software.

Thanks

-Thi

As another customer has just found out - Crystal reports does not save in MS Word format, it just saved in RTF format and gives .doc extension, see https://forum.aspose.com/t/129824. We will have support for RTF import in the near future.

You can insert table of contents field using [DocumentBuilder.InsertTableOfContents](https://forum.aspose.com/Products/Aspose.Word/Api/Aspose.Word.DocumentBuilder.InsertTableOfContents.html), but I must admit its usefulness is limited as when you open the file in MS Word, the fields are not automatically updated unless you select a field (or select all document) and hit F9.

//This is how to insert a TOC field.
Document doc = TestUtil.Open(@“DocBuilder\TestDocBuilderAdvancedTOC.doc”);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertTableOfContents(@"\o ““1-3"” \h \z \u”);
doc = TestUtil.SaveOpen(doc, @“DocBuilder\TestDocBuilderAdvancedTOC Out.doc”);

If you are okay that the recipient of the document needs to update the fields in the document on open, then that’s the way to go. If not, then you can try to “manually” create a table of contents for a document using DocumentBuilder. But this is of course difficult if you want the TOC to show page numbers as you don’t know page numbers unless your document is of some fixed layout.