Thursday, 20 February 2014

How to get nested table count in word document using Aspose.word

   
Calculate Nested Table count in word document using Aspose.word

        #region "Description"
        /*
          Nested table count can be checked using Aspose.word for Microsoft word documents using method given in this blog.
         */
        #endregion


Nested table Examples (Two nested tables):
1)     
2)             
3)             
4)             
5)             
6)             
7)           
8)             
9)             
10)          
11)          
12)        
13)          
14)          
15)          

        2)
A
B


A

 
Methods:

        /// <summary>
        /// Given Function takes input as Aspose word document object and returns the count of number of nested tables present in document
        /// </summary>
        /// <param name="doc">Aspose word document object 'doc'</param>
        /// <returns>nested table count '_NestedTables'</returns>
        public static int ValidateNestedTables(Aspose.Words.Document doc)
        {
            int _NestedTables = 0;
            bool isNestedTable = false;
            try
            {
                Aspose.Words.NodeCollection tableList = doc.GetChildNodes(Aspose.Words.NodeType.Table, true);
                foreach (Aspose.Words.Tables.Table table in tableList)
                {
                    isNestedTable = false;
                    if (table.Rows.Count > 0)
                    {
                        for (int row = 0; row < table.Rows.Count; row++)
                        {
                            for (int col = 0; col < table.Rows[row].Cells.Count; col++)
                            {
                                if (table.Rows[row].Cells[col].HasChildNodes)
                                {
                                    for (int child = 0; child < table.Rows[row].Cells[col].ChildNodes.Count; child++)
                                    {
                                        if (table.Rows[row].Cells[col].ChildNodes[child].NodeType == Aspose.Words.NodeType.Table)
                                        {
                                            isNestedTable = true; break;
                                        }
                                    }
                                }
                                if (isNestedTable) break;
                            }
                            if (isNestedTable) break;
                        }
                        if (isNestedTable)
                        {
                            _NestedTables += 1;
                        }
                    }
                }            
            }
            catch(Exception ex)
            {
            }
            return _NestedTables;
        }


1 comment: