Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Thu Feb 04, 2021 6:53 pm

Hi Team,

We are looking for the parent reference of any table present inside the DOCX. If the table is inside any Heading-1/2/3, we need that reference while reading the table data, similarly, if the table is present inside any numbering/bullet list, then we need the reference of that particular list level.

Please advise on the solution.

Thank you.

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

Fri Feb 05, 2021 11:38 am

Hello,

Thanks for your inquiry.
Please refer to the following code. If this is not what you want, please provide your input file and you desired output. Then we will investigate further.
Code: Select all
       static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("test.docx");
            Section section = doc.Sections[0];
            //Get the first table
            ITable table = section.Tables[0] as ITable;
            //Get the parent list
            Paragraph listPara = GetParentListLevel(table);
            //Get the parent heading
            Paragraph headingPara = GetParentHeading(table);
        }

        static Paragraph GetParentListLevel(ITable table)
        {
            Body owner = (Body)table.Owner;
            int v = owner.Tables.Count;
            int paraIndex = owner.ChildObjects.IndexOf(table);

            for (; paraIndex >= 0; paraIndex--)
            {
                if (owner.ChildObjects[paraIndex].DocumentObjectType == DocumentObjectType.Paragraph)
                {
                    Paragraph paragraph = (Paragraph)owner.ChildObjects[paraIndex];
                    if (paragraph.ListText != "")
                    {
                        return paragraph;
                    }
                   
                }
            }
            return null;
        }

        static Paragraph GetParentHeading(ITable table)
        {
            Body owner = (Body)table.Owner;
            int paraIndex = owner.ChildObjects.IndexOf(table) - 1;

            for (; paraIndex >= 0; paraIndex--)
            {
                if (owner.ChildObjects[paraIndex].DocumentObjectType == DocumentObjectType.Paragraph)
                {
                    Paragraph paragraph = (Paragraph)owner.ChildObjects[paraIndex];
                    OutlineLevel olevel = paragraph.Format.OutlineLevel;
                    for (int i = paraIndex; i >= 0; i--)
                    {
                        if (olevel == OutlineLevel.Level3)
                        {
                            return paragraph;
                        }
                        if (olevel == OutlineLevel.Level2)
                        {
                            return paragraph;
                        }
                        if (olevel == OutlineLevel.Level1)
                        {
                            return paragraph;
                        }
                    }
                }
            }
            return null;
        }


Sincerely,
Brian
E-iceblue support team
User avatar

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

Thu Feb 25, 2021 12:03 pm

Thanks Brian for the solution. This code is working fine.

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

Fri Feb 26, 2021 3:42 am

You are welcome.
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: 569
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc