by julien_ncit » Fri Jun 05, 2015 12:04 pm
Hi guys,
To remove tables, this code works as well :
- Code: Select all
public void removeTable(int indexTableToRemove) {
if (indexTableToRemove < 0)
throw new InvalidParameterException("indexTableToRemove is negative !")
Body body = this.wordMLPackage.getMainDocumentPart().getJaxbElement().getBody()
List<Object> listeTables = getAllElementFromObject(body, Tbl.class)
Tbl tableToRemove = listeTables[indexTableToRemove]
if (indexTableToRemove > listeTables.size())
throw new InvalidParameterException("indexTableToRemove exceed the number of tables contain in the document !")
body.getContent().remove(tableToRemove.getParent())
}
private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch)) {
result.add(obj);
}
if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}