GetText
Contenido
[
Ocultar
]Row.GetText method
Obtiene el texto de todas las celdas de esta fila, incluido el carácter de final de fila.
public override string GetText()
Observaciones
Devuelve el texto concatenado de todos los nodos secundarios con el carácter de final de fila Cell
adjuntado al final.
La cadena devuelta incluye todos los caracteres de control y especiales como se describe enControlChar
.
Ejemplos
Muestra cómo imprimir la estructura de nodos de cada tabla en un documento.
public void TableToText()
{
Document doc = new Document(MyDir + "DocumentVisitor-compatible features.docx");
TableStructurePrinter visitor = new TableStructurePrinter();
// Cuando conseguimos que un nodo compuesto acepte un visitante de documento, el visitante visita el nodo que lo acepta,
// y luego recorre todos los nodos secundarios en profundidad.
//El visitante puede leer y modificar cada nodo visitado.
doc.Accept(visitor);
Console.WriteLine(visitor.GetText());
}
/// <summary>
/// Recorre el árbol no binario de nodos secundarios de un nodo.
/// Crea un mapa en forma de cadena de todos los nodos de tabla encontrados y sus hijos.
/// </summary>
public class TableStructurePrinter : DocumentVisitor
{
public TableStructurePrinter()
{
mVisitedTables = new StringBuilder();
mVisitorIsInsideTable = false;
}
public string GetText()
{
return mVisitedTables.ToString();
}
/// <summary>
/// Se llama cuando se encuentra un nodo Ejecutar en el documento.
/// Las ejecuciones que no están dentro de las tablas no se registran.
/// </summary>
public override VisitorAction VisitRun(Run run)
{
if (mVisitorIsInsideTable) IndentAndAppendLine("[Run] \"" + run.GetText() + "\"");
return VisitorAction.Continue;
}
/// <summary>
/// Se llama cuando se encuentra una tabla en el documento.
/// </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>
/// Se llama después de que se hayan visitado todos los nodos secundarios de un nodo de tabla.
/// </summary>
public override VisitorAction VisitTableEnd(Table table)
{
mDocTraversalDepth--;
IndentAndAppendLine("[Table end]");
mVisitorIsInsideTable = false;
return VisitorAction.Continue;
}
/// <summary>
/// Se llama cuando se encuentra un nodo de fila en el documento.
/// </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>
/// Se llama después de que se hayan visitado todos los nodos secundarios de un nodo de fila.
/// </summary>
public override VisitorAction VisitRowEnd(Row row)
{
mDocTraversalDepth--;
IndentAndAppendLine("[Row end]");
return VisitorAction.Continue;
}
/// <summary>
/// Se llama cuando se encuentra un nodo Cell en el documento.
/// </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>
/// Se llama después de que se hayan visitado todos los nodos secundarios de un nodo Cell.
/// </summary>
public override VisitorAction VisitCellEnd(Cell cell)
{
mDocTraversalDepth--;
IndentAndAppendLine("[Cell end]");
return VisitorAction.Continue;
}
/// <summary>
/// Agrega una línea al StringBuilder y sangra según la profundidad del visitante
/// en el árbol de nodos secundarios de la tabla actual.
/// </summary>
/// <param name="texto"></param>
private void IndentAndAppendLine(string text)
{
for (int i = 0; i < mDocTraversalDepth; i++)
{
mVisitedTables.Append("| ");
}
mVisitedTables.AppendLine(text);
}
private bool mVisitorIsInsideTable;
private int mDocTraversalDepth;
private readonly StringBuilder mVisitedTables;
}
Ver también
- class Row
- espacio de nombres Aspose.Words.Tables
- asamblea Aspose.Words