riku
2022-06-17 3a5c011d9509d3bc0367921f463676c81ff2e374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.CommentFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.ContentAccessor;
import org.jvnet.jaxb2_commons.ppp.Child;
 
import java.util.List;
 
 
public class CommentsDeleter {
 
    public static void main(String[] args) throws Exception {
    
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                .load(new java.io.File(System.getProperty("user.dir") + "/CommentsDeleter.docx"));
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
 
        org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
                .getJaxbElement();
        Body body = wmlDocumentEl.getBody();
    
        CommentFinder cf = new CommentFinder();
        new TraversalUtil(body, cf);
    
        for (Child commentElement : cf.getCommentElements()) {
            System.out.println(commentElement.getClass().getName());
            Object parent = commentElement.getParent();
            List<Object> theList = ((ContentAccessor)parent).getContent();
            boolean removeResult = remove(theList, commentElement );
            System.out.println(removeResult);
        }    
    }
 
    private static boolean remove(List<Object> theList, Object bm)     {
        // Can't just remove the object from the parent,
        // since in the parent, it may be wrapped in a JAXBElement
        for (Object ox : theList) {
            if (XmlUtils.unwrap(ox).equals(bm)) {
                return theList.remove(ox);
            }
        }
        return false;
    }
    
}