Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Tables.MergedCells.SpanVisitor.SpanVisitor C# (CSharp) Method

SpanVisitor() public method

Creates new SpanVisitor instance
public SpanVisitor ( Document doc ) : System
doc Document Is document which we should parse
return System
            public SpanVisitor(Document doc)
            {
                // Get collection of tables from the document
                mWordTables = doc.GetChildNodes(NodeType.Table, true);

                // Convert document to HTML
                // We will parse HTML to determine rowspan and colspan of each cell
                MemoryStream htmlStream = new MemoryStream();

                HtmlSaveOptions options = new HtmlSaveOptions();
                options.ImagesFolder = Path.GetTempPath();

                doc.Save(htmlStream, options);

                // Load HTML into the XML document
                XmlDocument xmlDoc = new XmlDocument();
                htmlStream.Position = 0;
                xmlDoc.Load(htmlStream);

                // Get collection of tables in the HTML document
                XmlNodeList tables = xmlDoc.DocumentElement.SelectNodes("// Table");

                foreach (XmlNode table in tables)
                {
                    TableInfo tableInf = new TableInfo();
                    // Get collection of rows in the table
                    XmlNodeList rows = table.SelectNodes("tr");

                    foreach (XmlNode row in rows)
                    {
                        RowInfo rowInf = new RowInfo();

                        // Get collection of cells
                        XmlNodeList cells = row.SelectNodes("td");

                        foreach (XmlNode cell in cells)
                        {
                            // Determine row span and colspan of the current cell
                            XmlAttribute colSpanAttr = cell.Attributes["colspan"];
                            XmlAttribute rowSpanAttr = cell.Attributes["rowspan"];

                            int colSpan = colSpanAttr == null ? 0 : Int32.Parse(colSpanAttr.Value);
                            int rowSpan = rowSpanAttr == null ? 0 : Int32.Parse(rowSpanAttr.Value);

                            CellInfo cellInf = new CellInfo(colSpan, rowSpan);
                            rowInf.Cells.Add(cellInf);
                        }

                        tableInf.Rows.Add(rowInf);
                    }

                    mTables.Add(tableInf);
                }
            }
MergedCells.SpanVisitor