In both Word documents and in the Aspose.Words Document Object Model, there is no concept of a column. By design, table rows in Microsoft Word are completely independent and base properties and operations are only contained on rows and cells of the table. This gives tables the possibility of some interesting attributes:
· Each row in a table can have a completely different number of cells.
· Vertically, the cells of each row can have different widths.
· It is possible to join tables with differing row formats and cell counts.
Any operations that are performed on columns in Microsoft Word are in actual fact “short-cut methods” which perform the operation by modifying the cells of the rows collectively in such a way that it appears they are being applied to columns. This structure of rows and cells is the same way that tables are represented in Aspose.Words.
In the Aspose.Words Document Object Model a Table node is made up of Row and then Cell nodes. There is also no native support for columns.
You can still achieve such operations on columns by iterating through the same cell index of the rows of a table. The code below makes such operations easier by proving a façade class which collects the cells which make up a “column” of a table.
Example
Demonstrates a facade object for working with a column of a table.
[Java]
/**
* Represents a facade object for a column of a table in a Microsoft Word document.
*/
class Column
{
private Column(Table table, int columnIndex)
{
if (table == null)
throw new IllegalArgumentException("table");
mTable = table;
mColumnIndex = columnIndex;
}
/**
* Returns a new column facade from the table and supplied zero-based index.
*/
public static Column fromIndex(Table table, int columnIndex)
{
return new Column(table, columnIndex);
}
/**
* Returns the cells which make up the column.
*/
public Cell[] getCells()
{
ArrayList columnCells = getColumnCells();
return (Cell[])columnCells.toArray(new Cell[columnCells.size()]);
}
/**
* Returns the index of the given cell in the column.
*/
public int indexOf(Cell cell)
{
return getColumnCells().indexOf(cell);
}
/**
* Inserts a brand new column before this column into the table.
*/
public Column insertColumnBefore()
{
Cell[] columnCells = getCells();
if (columnCells.length == 0)
throw new IllegalArgumentException("Column must not be empty");
// Create a clone of this column.
for(Cell cell : columnCells)
cell.getParentRow().insertBefore(cell.deepClone(false), cell);
// This is the new column.
Column column = new Column(columnCells[0].getParentRow().getParentTable(), mColumnIndex);
// We want to make sure that the cells are all valid to work with (have at least one paragraph).
for (Cell cell : column.getCells())
cell.ensureMinimum();
// Increase the index which this column represents since there is now one extra column infront.
mColumnIndex++;
return column;
}
/**
* Removes the column from the table.
*/
public void remove()
{
for (Cell cell : getCells())
cell.remove();
}
/**
* Returns the text of the column.
*/
public String toTxt() throws Exception
{
StringBuilder builder = new StringBuilder();
for (Cell cell : getCells())
builder.append(cell.toString(SaveFormat.TEXT));
return builder.toString();
}
/**
* Provides an up-to-date collection of cells which make up the column represented by this facade.
*/
private ArrayList getColumnCells()
{
ArrayList columnCells = new ArrayList();
for (Row row : mTable.getRows())
{
Cell cell = row.getCells().get(mColumnIndex);
if (cell != null)
columnCells.add(cell);
}
return columnCells;
}
private int mColumnIndex;
private Table mTable;
}
Example
Shows how to insert a blank column into a table.
[Java]
// Get the second column in the table.
Column column = Column.fromIndex(table, 1);
// Create a new column to the left of this column.
// This is the same as using the "Insert Column Before" command in Microsoft Word.
Column newColumn = column.insertColumnBefore();
// Add some text to each of the column cells.
for (Cell cell : newColumn.getCells())
cell.getFirstParagraph().appendChild(new Run(doc, "Column Text " + newColumn.indexOf(cell)));
Example
Shows how to get the plain text of a table column.
[Java]
// Get the first column in the table.
Column column = Column.fromIndex(table, 0);
// Print the plain text of the column to the screen.
System.out.println(column.toTxt());
Example
Shows how to remove a column from a table in a document.
[Java]
Document doc = new Document(getMyDir() + "Table.Document.doc");
Table table = (Table)doc.getChild(NodeType.TABLE, 1, true);
// Get the third column from the table and remove it.
Column column = Column.fromIndex(table, 2);
column.remove();
doc.save(getMyDir() + "Table.RemoveColumn Out.doc");