Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Feb 04, 2021 6:49 pm

Hi Team,

We would like to read the parent/ancestor of the List Level Number for second level onwards. For example, if the numbering list tree is given as below, then while parsing the tree in docx, we need the immediate parent when we reach to any child node.

Example Tree:
1. List-1
2. List-2
2.1 List-2a
2.2 List-2b
2.3 List-2c
2.3.1 List-2c-a
2.3.2 List-2c-b
3. List-3

Scenario: When we reach at 2.2 "List-2b", we need immediate parent text as "List-2". Similarly, in case of 2.3.2 "List-2c-b", we would need to have the text of its parent i.e. "2.3 List-2c".

Please advice.

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Fri Feb 05, 2021 9:10 am

Hello,

Thanks for your inquiry.
Please refer to the following code to achieve your requirements. If there are any questions, please provide your input file for further investigation.
Code: Select all
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("test.docx");
            Section section = doc.Sections[0];
            //List-2c-b
            Paragraph paragraph = section.Body.Paragraphs[3];
            //Get the parent paragraph
            Paragraph parent = GetParent(paragraph);
            string text = parent.Text;
        }

        static Paragraph GetParent(Paragraph paragraph)
        {
            Body owner = (Body)paragraph.Owner;
            int paraIndex  = owner.Paragraphs.IndexOf(paragraph);
            String listname = paragraph.ListFormat.CurrentListStyle.Name;
            int listLevelNumber = paragraph.ListFormat.ListLevelNumber;
            for (; paraIndex >= 0; paraIndex--)
            {
                if (owner.Paragraphs[paraIndex].ListFormat.CurrentListStyle.Name == listname && owner.Paragraphs[paraIndex].ListFormat.ListLevelNumber == listLevelNumber - 1)
                {
                    return owner.Paragraphs[paraIndex];
                }
            }
            return null;
        }


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Fri Mar 05, 2021 4:14 am

Thanks Brian for your assistance. We are able to get the parent information now.

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Fri Mar 05, 2021 9:03 am

Glad to hear that.
If you encounter any issues related to our products in the future, please feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc