我的Word文档有两个表,我正在尝试使用以下代码删除最后一个表:
public static void removeTable() throws Docx4JException, JAXBException {
File doc = new File("D:\\Hello.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
String xpath = "//w:tbl";
List<Object> list = mainDocumentPart.getJAXBNodesViaXPath(xpath, false);
if(list.size()==2){
Tbl tbl = (Tbl) XmlUtils.unwrap(list.get(list.size()-1));
mainDocumentPart.getContent().remove(tbl.getParent());
wordMLPackage.save(new java.io.File("D:\\Hello.docx"));
System.out.println(list.size());
}
}
但是我的文档没有任何反应。在这方面有人可以帮助我吗?谢谢
答案 0 :(得分:0)
我使用this代码作为基础。
可行的解决方案:
public class RemoveLastTable {
public static void main(String[] args) throws Docx4JException {
File doc = new File("d:\\tmp\\tables.docx");
WordprocessingMLPackage pkg = WordprocessingMLPackage.load(doc);
removeLastTable(pkg, "d:\\tmp\\tables_updated.docx");
}
public static void removeLastTable(WordprocessingMLPackage wordMLPackage, String outFile) throws Docx4JException {
Body body = wordMLPackage.getMainDocumentPart().getContents().getBody();
List<Object> tables = getAllElementFromObject(body, Tbl.class);
int indexTableToRemove = tables.size() - 1;
Tbl tableToRemove = (Tbl) tables.get(indexTableToRemove);
body.getContent().remove(tableToRemove.getParent());
wordMLPackage.save(new File(outFile));
}
private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<>();
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;
}
}
但是,保存更新的文档并不完美,我的Word 2016(Office 365)仅在执行恢复后才能够读取结果。
答案 1 :(得分:0)
首先,在XPath返回的对象列表中指定要删除的项目。
Object deleteMe = list.get(1);
使用代码:
Object parent = getParent(deleteMe);
if (parent instanceof ContentAccessor) {
boolean result = ((ContentAccessor)parent).getContent().remove(deleteMe);
System.out.println("Deleted? " + result);
} else {
System.out.println("TODO: get content list from " + parent.getClass().getName());
}
带有一些辅助方法:
private Object getParent(Object o) {
return ((Child)XmlUtils.unwrap(o)).getParent();
}