0
votes

My word document has two tables and I am trying to delete last table with following code:

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());
    }
}

But nothing is happening to my document. Can anybody help me in this regard? Thanks

2

2 Answers

0
votes

I used this code as base.

A working solution:

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;
    }
}

However the saving of the updated document is not perfect, my Word 2016 (Office 365) was not able to read the result, only after doing recovery.

0
votes

First, specify the item you want to delete (in the list of objects your XPath returned).

 Object deleteMe = list.get(1);

Use the code:

        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());               
        }

with a little helper method:

private Object getParent(Object o) {    
    return ((Child)XmlUtils.unwrap(o)).getParent();
}