public class TableCollection extends NodeCollection
Table nodes.
To learn more, visit the Working with Tables documentation article.
Examples:
Shows how to remove the first and last rows of all tables in a document.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(5, tables.get(0).getRows().getCount());
Assert.assertEquals(4, tables.get(1).getRows().getCount());
for (Table table : tables) {
if (table.getFirstRow() != null) {
table.getFirstRow().remove();
}
if (table.getLastRow() != null) {
table.getLastRow().remove();
}
}
Assert.assertEquals(3, tables.get(0).getRows().getCount());
Assert.assertEquals(2, tables.get(1).getRows().getCount());
Shows how to find out if a tables are nested.
public void calculateDepthOfNestedTables() throws Exception {
Document doc = new Document(getMyDir() + "Nested tables.docx");
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (int i = 0; i < tables.getCount(); i++) {
Table table = (Table) tables.get(i);
// Find out if any cells in the table have other tables as children.
int count = getChildTableCount(table);
System.out.print(MessageFormat.format("Table #{0} has {1} tables directly within its cells", i, count));
// Find out if the table is nested inside another table, and, if so, at what depth.
int tableDepth = getNestedDepthOfTable(table);
if (tableDepth > 0)
System.out.println(MessageFormat.format("Table #{0} is nested inside another table at depth of {1}", i, tableDepth));
else
System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", i));
}
}
// Calculates what level a table is nested inside other tables.
//
// Returns An integer containing the level the table is nested at.
// 0 = Table is not nested inside any other table
// 1 = Table is nested within one parent table
// 2 = Table is nested within two parent tables etc..
private static int getNestedDepthOfTable(final Table table) {
int depth = 0;
Node parent = table.getAncestor(table.getNodeType());
while (parent != null) {
depth++;
parent = parent.getAncestor(Table.class);
}
return depth;
}
// Determines if a table contains any immediate child table within its cells.
// Does not recursively traverse through those tables to check for further tables.
//
// Returns true if at least one child cell contains a table.
// Returns false if no cells in the table contains a table.
private static int getChildTableCount(final Table table) {
int childTableCount = 0;
for (Row row : table.getRows()) {
for (Cell cell : row.getCells()) {
TableCollection childTables = cell.getTables();
if (childTables.getCount() > 0) childTableCount++;
}
}
return childTableCount;
}
| Modifier and Type | Method and Description |
|---|---|
Node |
get(int index)
Retrieves a
Table at the given index. |
Node[] |
toArray()
Copies all tables from the collection to a new array of tables.
|
add, clear, contains, getContainer, getCount, getCurrentNode, getNextMatchingNode, indexOf, insert, iterator, remove, removeAtpublic Node get(int index)
Table at the given index.
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
Examples:
Shows how to iterate through all tables in the document and print the contents of each cell.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(2, tables.toArray().length);
for (int i = 0; i < tables.getCount(); i++) {
System.out.println(MessageFormat.format("Start of Table {0}", i));
RowCollection rows = tables.get(i).getRows();
for (int j = 0; j < rows.getCount(); j++) {
System.out.println(MessageFormat.format("\tStart of Row {0}", j));
CellCollection cells = rows.get(j).getCells();
for (int k = 0; k < cells.getCount(); k++) {
String cellText = cells.get(k).toString(SaveFormat.TEXT).trim();
System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", k, cellText));
}
System.out.println(MessageFormat.format("\tEnd of Row {0}", j));
}
System.out.println(MessageFormat.format("End of Table {0}\n", i));
}
get in class NodeCollectionindex - An index into the collection.Table value.public Node[] toArray()
Examples:
Shows how to iterate through all tables in the document and print the contents of each cell.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(2, tables.toArray().length);
for (int i = 0; i < tables.getCount(); i++) {
System.out.println(MessageFormat.format("Start of Table {0}", i));
RowCollection rows = tables.get(i).getRows();
for (int j = 0; j < rows.getCount(); j++) {
System.out.println(MessageFormat.format("\tStart of Row {0}", j));
CellCollection cells = rows.get(j).getCells();
for (int k = 0; k < cells.getCount(); k++) {
String cellText = cells.get(k).toString(SaveFormat.TEXT).trim();
System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", k, cellText));
}
System.out.println(MessageFormat.format("\tEnd of Row {0}", j));
}
System.out.println(MessageFormat.format("End of Table {0}\n", i));
}
toArray in class NodeCollection