feiyu02
2022-11-15 23bd719cebe5feeff4e48fde925b0b39755eea93
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cn.flightfeather.supervision.docx4j.simpleDemo;
 
import org.docx4j.Docx4J;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.finders.RangeFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.ContentAccessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.List;
 
 
public class BookmarksDeleter {
    
    protected static Logger log = LoggerFactory.getLogger(BookmarksDeleter.class);
    
 
    public static void main(String[] args) throws Exception {
 
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
                .load(new java.io.File(System.getProperty("user.dir")
                        + "/BookmarksDeleter.docx"));
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
        
        // Before..
        // System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
 
        org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
                .getJaxbElement();
        Body body = wmlDocumentEl.getBody();
 
        fixRange(body.getContent(), "CTBookmark", "CTMarkupRange");
        
        // After
//        System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
        
        // or save the docx...
        Docx4J.save(wordMLPackage, new java.io.File(System.getProperty("user.dir")
                        + "/OUT_BookmarksDeleter.docx"));
    }
 
    private static void fixRange(List<Object> paragraphs, String startElement,
            String endElement) throws Exception {
 
        RangeFinder rt = new RangeFinder(startElement, endElement);
        new TraversalUtil(paragraphs, rt);
        
        for (CTBookmark bm : rt.getStarts()) {
            try {
                // Can't just remove the object from the parent,
                // since in the parent, it may be wrapped in a JAXBElement
                List<Object> theList = null;
                if (bm.getParent() instanceof List) {
                    theList = (List)bm.getParent(); // eg body.getContent()
                } else {
                    theList = ((ContentAccessor)(bm.getParent())).getContent();
                }
                Object deleteMe = null;
                for (Object ox : theList) {
                    if (XmlUtils.unwrap(ox).equals(bm)) {
                        deleteMe = ox;
                        break;
                    }
                }
                if (deleteMe!=null) {
                    theList.remove(deleteMe);                        
                }
            } catch (ClassCastException cce) {
                log.error(cce.getMessage(), cce);
            }
        }
 
        for (CTMarkupRange mr : rt.getEnds()) {
            try {
                // Can't just remove the object from the parent,
                // since in the parent, it may be wrapped in a JAXBElement
                List<Object> theList = null;
                if (mr.getParent() instanceof List) {
                    theList = (List)mr.getParent(); // eg body.getContent()
                } else {
                    theList = ((ContentAccessor)(mr.getParent())).getContent();
                }
                Object deleteMe = null;
                for (Object ox : theList) {
                    if (XmlUtils.unwrap(ox).equals(mr)) {
                        deleteMe = ox;
                        break;
                    }
                }
                if (deleteMe!=null) {
                    theList.remove(deleteMe);                        
                }
            } catch (ClassCastException cce) {
                log.info(mr.getParent().getClass().getName());
                log.error(cce.getMessage(), cce);
            }
        }
        
        // NB: this leaves begin|separate|end behind!
        // It would be better to delete the whole field, or just leave it,
        // so this is commented out.  Result in Word will be "bookmark undefined!"
//        for (Text instrText : rt.refs) {
//            try {
//                List<Object> theList = ((ContentAccessor)(instrText.getParent())).getContent();
//                Object deleteMe = null;
//                for (Object ox : theList) {
//                    if (XmlUtils.unwrap(ox).equals(instrText)) {
//                        deleteMe = ox;
//                        break;
//                    }
//                }
//                if (deleteMe!=null) {
//                    theList.remove(deleteMe);                        
//                }
//            } catch (ClassCastException cce) {
//                log.info(instrText.getParent().getClass().getName());
//                log.error(cce);
//            }
//        }
        
    }
 
 
    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;
    }
    
}