Dear,
We would like to use Aspose.Word product to extract the content of the cells of a Tables from DOCX document. Is there any way to know the type of content of a cell, to proceed to extract it as an image, text or formula?
I show an example I am trying to do:
String foldername = "applications/classification/smoketest";
String filename = "Description-Large-Tables.docx";
Document document = TestUtils.loadAsposeDocxDocument(foldername, filename);
List<com.aspose.words.Table> officeTableList = AsposeDocumentUtils.getTables(document);
List<org.epo.filing.docxconversion.st36.content.Table> tableObjectList = new ArrayList<>();
for (com.aspose.words.Table officeTable : officeTableList) {
tableObjectList.add((org.epo.filing.docxconversion.st36.content.Table)
renderOfficeTableToST36ElementAsObject(officeTable));
}
/**
* Creating a ST36 table and setting the content from the Aspose table.
*/
public Object renderOfficeTableToST36ElementAsObject(final com.aspose.words.Table officeTable) {
try {
org.epo.filing.docxconversion.st36.content.Table table = new org.epo.filing.docxconversion.st36.content.Table();
//Separation of rows and columns //spacing or width?
double spacing = officeTable.getCellSpacing();
//double width = officeTable.getPreferredWidth().getValue();
table.setColsep(String.valueOf(spacing));
table.setRowsep(String.valueOf(spacing));
//Group of columns
Tgroup tgroup1 = new Tgroup();
//Body with rows
Tbody tbody = new Tbody();
int rowNumber = officeTable.getRows().getCount();
int columnNumber = 0;
// Iterate through all rows in the table
for (int i = 0; i < rowNumber; i++) {
Row row = officeTable.getRows().get(i);
org.epo.filing.docxconversion.st36.content.Row tbodyRow = new org.epo.filing.docxconversion.st36.content.Row();
int cellNumber = row.getCells().getCount();
//Check if the number of cells is the same as the number of cells in the other rows,
// if it is larger, add the corresponding columns to the new table
if (cellNumber > columnNumber) {
//Create a column for each cell
for (int j = columnNumber; j < cellNumber; j++) {
Cell cell = row.getCells().get(j);
Colspec colspec = new Colspec();
colspec.setColname("C0" + j);
colspec.setColnum("" + j);
colspec.setColwidth(Double.toString(cell.getCellFormat().getWidth()));
tgroup1.getColspec().add(colspec);
}
columnNumber = cellNumber;
}
// Iterate through all cells in the row
for (int j = 0; j < cellNumber; j++) {
Cell cell = row.getCells().get(j);
int cellNodeType = cell.getNodeType(); //Allways return 7
String texto = cell.getText();
String cellText = cell.toString(SaveFormat.TEXT).trim();
Entry entry = new Entry();
entry.setNamest("C0" + j);
entry.getContent().add(cellText);
tbodyRow.getEntry().add(entry);
}
tbody.getRow().add(tbodyRow);
}
tgroup1.setCols(Integer.toString(columnNumber));
tgroup1.setTbody(tbody);
table.getTgroup().add(tgroup1);
return table;
} catch (Exception e) {
LOGGER.error(TABLE_ML_CONVERSION_EXCEPTION, e);
return null;
}
}
}
A lot of thanks!