Reading the table of content with parent child relations in Dot Net

Hi,
I have a requirement of reading the table of content from word document and store the table of content in a data table and display using a Tree View using parent and child relation same like table of content in word document.

Here i am unable to get the parent and child relations to make a tree view.
How to get the parent and child relations?

Thanks & Regards
Charan.

Hi Charan,

Thanks for your inquiry. Please attach your input Word document you want to read TOC content from and expected file showing the final TreeView output here for our reference. We will investigate the issue on our end and provide you more information.

Best regards,

Hi,
Please find the attached document with table of contents.

Thanks & Regards
Charan

Hi Charan,

Thanks for your inquiry. Well, in your case, every node of TOC field is represented by a HYPERLINK field. When you click on a TOC item, the control jumps to a particular location in your document pointed by a hidden Bookmark. I think, you can implement the following workflow to achieve what you’re looking for:

Document doc = new Document(MyDir + @"TOC_Document.docx");
foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        if (hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
            if (tocItem != null)
            {
                // do something
                if (tocItem.ParagraphFormat.LeftIndent == 12)
                    Console.Write("\t");
                Console.WriteLine(tocItem.ToString(SaveFormat.Text));
            }
        }
    }
}

I hope, this helps.

Best regards,