We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Multilevel TOC

Dear Sir or Madam,

Currently we build TOC by adding TC entries like this:

string fieldBody = string.Format("TC \"{0}\"\\f x", "Section1");
documentBuilder.InsertField(fieldBody);
...
fieldBody = string.Format("TC \"{0}\"\\f x", "Section2");
documentBuilder.InsertField(fieldBody);
...
fieldBody = string.Format("TC \"{0}\"\\f x", "Note1");
documentBuilder.InsertField(fieldBody);

and then insert TOC field:

db.InsertField(string.Format("TOC \\f  x" );

How can I do a multilevel TOC (see an image attached)?

Thank you in advance,
Regards,
AlexToc.PNG (11.9 KB)

@AlexanderNovickov You can use "\l" switch in the TC field to specify level. Please see TC field documentation for more information. Please see the following simple code example:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a TOC at the beginning.
builder.InsertField(@"TOC \f  x");
builder.InsertBreak(BreakType.PageBreak);

// Insert few items;
builder.InsertField(string.Format("TC \"{0}\"\\f x \\l 1", "First level"));
builder.Writeln("First level");

builder.InsertField(string.Format("TC \"{0}\"\\f x \\l 2", "Second level"));
builder.Writeln("Second level");

builder.InsertField(string.Format("TC \"{0}\"\\f x \\l 3", "Third level"));
builder.Writeln("Third level");

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

The result looks like this:

Thank you Alexey, it’s what I was looking for.

Alex

1 Like