How to find and remove TOC from word document using aspose .word .net

Hi Team,
How to find and remove TOC from word document using Aspose .word .net

@thiru1711

Following code example shows how to find and remove the table of content from the document.

Document doc = new Document(MyDir + "in.docx");
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldTOC)
        field.Remove();
} 
doc.Save(MyDir + "output.docx");
1 Like

Hi @tahir.manzoor,
how to add and temove TOC with (toc header like Table of content) in word document using aspose.word

Toc Header text fromat refer screen shot: TOC_Insert_format.png (52.7 KB)

Sample Expected out put document :Sample Doc.zip (18.1 KB)

@thiru1711

You can use DocumentBuilder.InsertTableOfContents method to insert a TOC (table of contents) field into the document. For example, after creating a table of contents, the following field is inserted into the document: { TOC \o “1-3” \h \z \u }. You can copy \o “1-3” \h \z \u and use it as the switches parameter.

Following code example shows how to insert a Table of contents (TOC) into a document using heading styles as entries.

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

// Insert a table of contents at the beginning of the document,
// and set it to pick up paragraphs with headings of levels 1 to 3 and entries to act like hyperlinks
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");

// Start the actual document content on the second page
builder.InsertBreak(BreakType.PageBreak);

// Build a document with complex structure by applying different heading styles thus creating TOC entries
// The heading levels we use below will affect the list levels in which these items will appear in the TOC,
// and only levels 1-3 will be picked up by our TOC due to its settings
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 1.1");
builder.Writeln("Heading 1.2");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Heading 2");
builder.Writeln("Heading 3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.1");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.Writeln("Heading 3.1.1");
builder.Writeln("Heading 3.1.2");
builder.Writeln("Heading 3.1.3");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("Heading 3.2");
builder.Writeln("Heading 3.3");

// Call the method below to update the TOC and save
doc.UpdateFields();
doc.Save(ArtifactsDir + "DocumentBuilder.InsertToc.docx");

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.