Creating Table of Content in word document

Hi
Please let me know in case there is a way to create “Table of Content” in Word Document using aspose.
Thanks
Phani

Hi
Thanks for your request. Yes, of course, please see the following simple code:

Document doc = new Document("C:\\Temp\\in.doc");
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
// At the beggining of the document we insert TOC filed, whick will be updated before saving document.
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
doc.UpdateFields();
doc.Save("C:\\Temp\\out.doc");

Best regards,

Hi
Thanks for the Reply!
In the Document we have Multiple Tables and we need to get table names in TOC as mentioned below.
builder.InsertTableOfContents("\f")
Kindly let me know in case there is any way to set the Table Name/Text for a Table at Run Time using Aspose.
or
Please let me know if we can manually Create Table of Contents using Aspose.
Thanks
Phani

Hi
Thanks for your request. Unfortunately, it is not quite clear for me what you would like to achieve. Could you please attach you input and expected output documents here for testing? I will check them and provide you more information.
Best regards,

Hi
What i am looking for is adding the table of Contents manually using aspose.
Thanks
Phani

Hi
Thanks for your request. Actually, In MS Word table cannot have a name. In Ms Word you can specify a caption of a table. And then use these captions to built a TOT (Table of Tables). Is this what you would like to achieve?
The code provided by Andrey builds a TOC in the document by Headings. If you need to build a TOT, you should use code like the following:

builder.InsertTableOfContents("\\h \\z \\c \"Table\"");

Where “Table” is a name of SEQ field used for table’s caption.
It would be great if you attach a sample document where you need to insert TOC and the expected output.
Best regards,

Hi
Thanks for the Reply!
Could you kindly let me know if we could dynamically set the Caption of a table using aspose, so that we can use that in populating Table of Contents.
Thanks
Phani

Hi Phani,
Thanks for your request. Sure, you can. But maybe in your case it is not necessary. Please attach your sample document where you need to build a TOC. We will check it and provide you sample code that does what you need. Waiting for your inputs.
Best regards,

Hi
Thanks for the Reply!
Please find attached the Document. Where we need to populate Table of Contents dynamically by reading the Table Caption.
Thanks
Phani

Hi
Thank you for additional information. Since, you are building a document dynamically using Mail Merge, you can just put a Heading paragraph at the top of you regions and insert a mergefield into this paragraph where you will insert a table title. In this case you will be able to build a TOC using these heading paragraphs. I created a simple template and code example for you:

// Create a dummy datasource.
DataTable data = new DataTable("Data");
data.Columns.Add("Title");
for (int i = 0; i <10; i++)
    data.Rows.Add(new object[]
    {
        string.Format("Title_{0}", i)
    });
// Open template and execute mail merge.
Document doc = new Document(@"Test001\in.doc");
doc.MailMerge.ExecuteWithRegions(data);
// Update fields in the document to update TOC.
doc.UpdateFields();
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards,

The issues you have found earlier (filed as WORDSJAVA-1) have been fixed in this .NET update and in this Java update.