Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:12:15+00:00 2026-06-18T14:12:15+00:00

I’d like to remove all the comments from a docx file using docx4j. I

  • 0

I’d like to remove all the comments from a docx file using docx4j.

I can remove the actual comments with a piece of code like is shown below, but I think I also need to remove the comment references from the main document part as well (otherwise the document is corrupted), but I can’t figure out how to do that.

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

Any guidance appreciated!

I also posted this question on the docx4j forum: http://www.docx4java.org/forums/docx-java-f6/how-to-remove-all-comments-from-docx-file-t1329.html.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:12:17+00:00Added an answer on June 18, 2026 at 2:12 pm

    You can use TraversalUtil to find the relevant objects (CommentRangeStart, CommentRangeEnd, R.CommentReference), and then remove them.

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

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I'm using an ASP request returning a XML file containing some latin characters. By
I am using jsonparser to parse data and images obtained from json response. When
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.