@JamieAspose
Thanks for your feedback. You are using a quite old version. It is recommended to use latest version of Aspose.Words, as we add all the fixes and enhancement in latest version of Aspose.Words. In your stated version, you can try following code snippet to remove the TOC Field.
Document doc = new Document("Remove-TC-Fields-Test.docx");
NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart start in starts)
{
if (start.FieldType == FieldType.FieldTOC)
{
Node curNode = start;
while (!(curNode.NodeType == NodeType.FieldEnd && ((FieldEnd)curNode).FieldType == FieldType.FieldTOC))
{
curNode = curNode.NextPreOrder(start.Document);
}
RemoveSequence(start, curNode);
start.Remove();
curNode.Remove();
break;
}
}
doc.Save(@"out.docx");
...............................
public static void RemoveSequence(Node start, Node end)
{
Node curNode = start.NextPreOrder(start.Document);
while (curNode != null && !curNode.Equals(end))
{
//Move to next node
Node nextNode = curNode.NextPreOrder(start.Document);
//Check whether current contains end node
if (curNode.IsComposite)
{
CompositeNode curComposite = (CompositeNode)curNode;
if (!curComposite.GetChildNodes(NodeType.Any, true).Contains(end) &&!curComposite.GetChildNodes(NodeType.Any, true).Contains(start))
{
nextNode = curNode.NextSibling;
curNode.Remove();
}
}
else
{
curNode.Remove();
}
curNode = nextNode;
}
}