Free Support Forum - aspose.com

Embedded tables causing exception in moveToCell command

Hi,

I’m attempting to parse a word document that is essentially a template of tables. On embedding a nested table in a cell of one of the tables, I experience an exception executing the moveToCell method of DocumentBuilder: java.lang.IndexOutOfBoundsException. Its appears as if there is a difference in the way moveToCell interpretes its table index parameter and the table index parameter produced by descending through the document sections and tables. The moveToCell method actually grabs the nested table with a table index associated with one of the template tables. The nested table has 10 rows in my sample. Here’s my code:

SectionCollection sectionCollection = document.getSections();

for (Section sect : sectionCollection) {

Integer sectionIndex = sectionCollection.indexOf(sect);
System.out.println("sectionIndex: " + sectionIndex);

documentBuilder.moveToSection(sectionIndex);
TableCollection tableCollection = sect.getBody().getTables();

for (Table table : tableCollection) {

Integer tableIndex = tableCollection.indexOf(table);
System.out.println("tableIndex: " + tableIndex);
System.out.println("0CellTitle: " + getCellText(table, 0, 0).trim());
System.out.println("7CellTitle: " + getCellText(table, 7, 0).trim());
System.out.println("7CellValue: " + getCellText(table, 7, 1).trim());
System.out.println("17CellTitle: " + getCellText(table, 17, 0).trim());

for (int rowIndex = 0; rowIndex <= 17; rowIndex++)
{
try
{
documentBuilder.moveToCell(tableIndex, rowIndex, 0, 0);
}
catch (Exception e)
{
throw new RuntimeException("failed to move to rowIndex " + rowIndex + " on table " + tableIndex, e);
}
}
}
}

Output:

You can see there is cell data at row indices 0,7,17 but moveToCell is unable to move to the second table’s row index 17 for some reason in the following output.

sectionIndex: 0
tableIndex: 0
0CellTitle: Question ID
7CellTitle: * Question Text:
7CellValue: first one
17CellTitle: Author Notes
tableIndex: 1
0CellTitle: Question ID
7CellTitle: * Question Text:
7CellValue: second one
17CellTitle: Author Notes
2010-04-19 17:58:46,741 com.kf.actions.EditorUploadQuestionSubmitAction DEBUG->Exception: java.lang.RuntimeException: failed to move to rowIndex 10 on table 1
java.lang.RuntimeException: failed to move to rowIndex 10 on table 1
Caused by: java.lang.IndexOutOfBoundsException: rowIndex
at com.aspose.words.DocumentBuilder.ak(DocumentBuilder.java:1788)
at com.aspose.words.DocumentBuilder.moveToCell(DocumentBuilder.java:366)

Why is it grabbing the embedded table? Any help very much appreciated!

Attached is my word template

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. The problem occurs because you use sect.getBody().getTables() to get a collection of table in the section. sect.getBody().getTables() returns only tables, which are direct children of Body. But in your case, you need also nested tables. I modified your code:

SectionCollection sectionCollection = document.getSections();

for (Section sect : sectionCollection)

{

Integer sectionIndex = sectionCollection.indexOf(sect);

System.out.println("sectionIndex: " + sectionIndex);

documentBuilder.moveToSection(sectionIndex);

NodeCollection tableCollection = sect.getChildNodes(NodeType.TABLE, true);

for (int tableIndex = 0; tableIndex<tableCollection.getCount(); tableIndex++)

{

Table table = (Table)tableCollection.get(tableIndex);

System.out.println("tableIndex: " + tableIndex);

for (int rowIndex = 0; rowIndex < table.getRows().getCount(); rowIndex++)

{

try

{

documentBuilder.moveToCell(tableIndex, rowIndex, 0, 0);

}

catch (Exception e)

{

throw new RuntimeException("failed to move to rowIndex " + rowIndex + " on table " + tableIndex, e);

}

}

}

}

Hope this helps.

Best regards.

Thank you Alexey! Just what I needed.

Jason