DocumentVisitorVisitTableStart Method |
Namespace: Aspose.Words
public void TableToText() { // Open the document that has tables we want to print the info of Document doc = new Document(MyDir + "DocumentVisitor-compatible features.docx"); // Create an object that inherits from the DocumentVisitor class TableInfoPrinter visitor = new TableInfoPrinter(); // Accepting a visitor lets it start traversing the nodes in the document, // starting with the node that accepted it to then recursively visit every child doc.Accept(visitor); // Once the visiting is complete, we can retrieve the result of the operation, // that in this example, has accumulated in the visitor Console.WriteLine(visitor.GetText()); } /// <summary> /// This Visitor implementation prints information about and contents of all tables encountered in the document. /// </summary> public class TableInfoPrinter : DocumentVisitor { public TableInfoPrinter() { mBuilder = new StringBuilder(); mVisitorIsInsideTable = false; } /// <summary> /// Gets the plain text of the document that was accumulated by the visitor. /// </summary> public string GetText() { return mBuilder.ToString(); } /// <summary> /// Called when a Run node is encountered in the document. /// </summary> public override VisitorAction VisitRun(Run run) { // We want to print the contents of runs, but only if they consist of text from cells // So we are only interested in runs that are children of table nodes if (mVisitorIsInsideTable) IndentAndAppendLine("[Run] \"" + run.GetText() + "\""); return VisitorAction.Continue; } /// <summary> /// Called when a Table is encountered in the document. /// </summary> public override VisitorAction VisitTableStart(Table table) { int rows = 0; int columns = 0; if (table.Rows.Count > 0) { rows = table.Rows.Count; columns = table.FirstRow.Count; } IndentAndAppendLine("[Table start] Size: " + rows + "x" + columns); mDocTraversalDepth++; mVisitorIsInsideTable = true; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Table node is ended. /// </summary> public override VisitorAction VisitTableEnd(Table table) { mDocTraversalDepth--; IndentAndAppendLine("[Table end]"); mVisitorIsInsideTable = false; return VisitorAction.Continue; } /// <summary> /// Called when a Row node is encountered in the document. /// </summary> public override VisitorAction VisitRowStart(Row row) { string rowContents = row.GetText().TrimEnd(new []{ '\u0007', ' ' }).Replace("\u0007", ", "); int rowWidth = row.IndexOf(row.LastCell) + 1; int rowIndex = row.ParentTable.IndexOf(row); string rowStatusInTable = row.IsFirstRow && row.IsLastRow ? "only" : row.IsFirstRow ? "first" : row.IsLastRow ? "last" : ""; if (rowStatusInTable != "") { rowStatusInTable = $", the {rowStatusInTable} row in this table,"; } IndentAndAppendLine($"[Row start] Row #{++rowIndex}{rowStatusInTable} width {rowWidth}, \"{rowContents}\""); mDocTraversalDepth++; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Row node is ended. /// </summary> public override VisitorAction VisitRowEnd(Row row) { mDocTraversalDepth--; IndentAndAppendLine("[Row end]"); return VisitorAction.Continue; } /// <summary> /// Called when a Cell node is encountered in the document. /// </summary> public override VisitorAction VisitCellStart(Cell cell) { Row row = cell.ParentRow; Table table = row.ParentTable; string cellStatusInRow = cell.IsFirstCell && cell.IsLastCell ? "only" : cell.IsFirstCell ? "first" : cell.IsLastCell ? "last" : ""; if (cellStatusInRow != "") { cellStatusInRow = $", the {cellStatusInRow} cell in this row"; } IndentAndAppendLine($"[Cell start] Row {table.IndexOf(row) + 1}, Col {row.IndexOf(cell) + 1}{cellStatusInRow}"); mDocTraversalDepth++; return VisitorAction.Continue; } /// <summary> /// Called when the visiting of a Cell node is ended in the document. /// </summary> public override VisitorAction VisitCellEnd(Cell cell) { mDocTraversalDepth--; IndentAndAppendLine("[Cell end]"); return VisitorAction.Continue; } /// <summary> /// Append a line to the StringBuilder and indent it depending on how deep the visitor is into the document tree. /// </summary> /// <param name="text"></param> private void IndentAndAppendLine(string text) { for (int i = 0; i < mDocTraversalDepth; i++) { mBuilder.Append("| "); } mBuilder.AppendLine(text); } private bool mVisitorIsInsideTable; private int mDocTraversalDepth; private readonly StringBuilder mBuilder; }