Get Page Number as well as Content from TOC - Table of Contents

Hi Team,

I need the page number as well as content from TOC. For example, below is the TOC

1: To Grant leave of absence…3
2: To confirm presence of Quorum…4

I need the these values : “To Grant leave of absence” and its page number “3”
“To confirm presence of Quorum” and its page number “4”.

Do we have any method or logic to get above details.

Thank you…

@Pintutrnt,

Please ZIP and upload your simplified input Word document here for testing. Please also provide a screenshot highlighting the areas in this document that you want to retrieve by using Aspose.Words. We will then investigate the scenario on our end and provide you more information.

Hi Awais,

I need the method information. I don’ t think , we need any document or screen shots to reply my logic.

@Pintutrnt,

Please see this sample Word document (TOC sample.zip (5.5 KB)) and try running the following code:

Document doc = new Document("E:\\temp\\TOC sample.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)
            {
                foreach (Field nestedField in tocItem.Range.Fields)
                {
                    if (nestedField.Type.Equals(FieldType.FieldPageRef))
                    {
                        // Work with page numbers here
                        // we will remove it
                        // nestedField.Remove();
                    }
                }

                Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim());
            }
        }
    }
}