当前位置: 首页 > 知识库问答 >
问题:

如何使用docx4j从docx文件中删除所有注释?

翟弘
2023-03-14

我想使用docx4j删除docx文件中的所有注释。

我可以使用如下所示的一段代码删除实际的注释,但我认为我也需要从主文档部分删除注释引用(否则文档已损坏),但我不知道如何做到这一点。

CommentsPart cmtsPart = wordMLPackage.getMainDocumentPart().getCommentsPart();
org.docx4j.wml.Comments cmts = cpart.getJaxbElement();
List<Comments.Comment> coms = cmts.getComment();
coms.clear();

感谢您的指导!

我还在docx4j论坛上发布了这个问题:http://www.docx4java.org/forums/docx-java-f6/how-to-remove-all-comments-from-docx-file-t1329.html.

谢谢

共有1个答案

司马晋
2023-03-14

可以使用TraversalUtil查找相关对象(CommentRangeStart、CommentRangeEnd、R.CommentReference),然后删除它们。

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBElement;

import org.docx4j.TraversalUtil;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.CommentRangeEnd;
import org.docx4j.wml.CommentRangeStart;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.R.CommentReference;
import org.jvnet.jaxb2_commons.ppp.Child;


public class Foo {
public static void main(String[] args) throws Exception {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .load(new java.io.File(System.getProperty("user.dir") + "/Foo.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.commentElements) {
        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;
}

static class CommentFinder extends CallbackImpl {

    List<Child> commentElements = new ArrayList<Child>();

    @Override
    public List<Object> apply(Object o) {

        if (o instanceof javax.xml.bind.JAXBElement
                && (((JAXBElement)o).getName().getLocalPart().equals("commentReference")
                        || ((JAXBElement)o).getName().getLocalPart().equals("commentRangeStart")
                        || ((JAXBElement)o).getName().getLocalPart().equals("commentRangeEnd")                                      
                        )) {
                System.out.println(((JAXBElement)o).getName().getLocalPart());
                commentElements.add( (Child)XmlUtils.unwrap(o) );
            } else 
        if (o instanceof CommentReference || 
            o instanceof CommentRangeStart || 
            o instanceof CommentRangeEnd) {
            System.out.println(o.getClass().getName());
            commentElements.add((Child)o);
        }
        return null;
    }

        @Override // to setParent
        public void walkJAXBElements(Object parent) {

            List children = getChildren(parent);
            if (children != null) {

                for (Object o : children) {

                    if (o instanceof javax.xml.bind.JAXBElement
                            && (((JAXBElement)o).getName().getLocalPart().equals("commentReference")
                                    || ((JAXBElement)o).getName().getLocalPart().equals("commentRangeStart")
                                    || ((JAXBElement)o).getName().getLocalPart().equals("commentRangeEnd")                                      
                                    )) {

                        ((Child)((JAXBElement)o).getValue()).setParent(XmlUtils.unwrap(parent));
                    } else {                        
                        o = XmlUtils.unwrap(o);
                        if (o instanceof Child) {
                            ((Child)o).setParent(XmlUtils.unwrap(parent));
                        }
                    }


                    this.apply(o);

                    if (this.shouldTraverse(o)) {
                        walkJAXBElements(o);
                    }

                }
            }
        }           
    }   

}

 类似资料: