Removing TOC

Hi,

I am reading a document with Document class, that document having updated TOC, i want to save the document in HTML/MHTML format, before saving i need to remove TOC, is there any method to remove the TOCs in the document before saving into another format.

Regards,
Srinu Dhulipalla

Hi

Thanks for your inquiry. TOC is actually the field. Every field in the Word document starts from FieldStart and ends with FieldEnd nodes. Therefore, to remove TOC from the document you should remove all content between these nodes. Here is code example:

public void Test018()
{
    // Open document
    Document doc = new Document(@"Test018\in.doc");
    // TOC is actualy a field, so we should find this field and remove it
    // Every field starts from FieldStart node and ends with FieldEnd
    // Get collection of FieldStart nodes from the document
    NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);
    // Loop throught collection and search for TOC field start
    foreach (FieldStart start in starts)
    {
        if (start.FieldType == FieldType.FieldTOC)
        {
            // We should remove all content between FieldStart and FieldEnd node
            // Search for end of the TOC field
            Node curNode = start;
            while (!(curNode.NodeType == NodeType.FieldEnd && ((FieldEnd)curNode).FieldType == FieldType.FieldTOC))
            {
                // Move to next node
                curNode = curNode.NextPreOrder(start.Document);
            }
            // Remove content between field start and field end
            RemoveSequence(start, curNode);
            // Remove start and end nodes
            try
            {
                start.Remove();
                curNode.Remove();
            }
            catch { }
            // Exit fromthe loop
            break;
        }
    }
    // Save the document
    doc.Save(@"Test018\out.doc");
}
/// 
/// Remove all nodes between start and end nodes, except start and end nodes
/// 
/// The start node
/// The end node
private 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)
        {
            if (!(curNode as CompositeNode).ChildNodes.Contains(end) &&
            !(curNode as CompositeNode).ChildNodes.Contains(start))
            {
                nextNode = curNode.NextSibling;
                curNode.Remove();
            }
        }
        else
        {
            curNode.Remove();
        }
        curNode = nextNode;
    }
}

Hope this helps.
Best regards.