Page 1 of 1

How to add comment to existing docx

PostPosted: Sun Dec 27, 2015 1:43 am
by harsha
Hello I am trying to add comment to a docx document if a table is not having caption in its immediate next paragraph tag.
code
Code: Select all
public boolean validateTableForCaption(Tbl table) throws Exception{
      int index = content.indexOf(table.getParent());
      //check immediate next element is paragraph containing caption
      Object nextContent = content.get(++index);
      
      if(nextContent instanceof P){
         P nextPara = (P) nextContent;
         PPr paraProperties = nextPara.getPPr();
         if(paraProperties == null){
            return false;
         }else{
            PStyle style = paraProperties.getPStyle();
            if(style == null){
               return false;
            }else if(style.getVal().equals("Caption")){
               StringWriter strWriter = new StringWriter();
               TextUtils.extractText(nextPara, strWriter);
               if(strWriter.toString().startsWith("Table ")){
                  return true;
               }else{
                  return false;
               }
            }
         }
      }
      
      return false;
   }


Test class to add new comment. I have used samples given in github link .
Only difference is I am not using context and objectfactory . I am creating objects with new.

Code: Select all
CommentsPart commentsPart = new CommentsPart();
      
      docx.getWordMLPackage().addTargetPart(commentsPart);
      
      Comments comments = new Comments();
      commentsPart.setJaxbElement(comments);
      
      for(Tbl table : docx.getAllTables()){
         if(!docx.validateTableForCaption(table)){
            commentId = BigInteger.valueOf(0);
            CommentRangeStart commentRangeStart = new CommentRangeStart();
            commentRangeStart.setId(commentId);
            Comment theComment = createComment(commentId, "fred", null,"caption absent");
            comments.getComment().add(theComment);
            CommentRangeEnd commentRangeEnd = new CommentRangeEnd();
            commentRangeEnd.setId(commentId);
            table.getContent().add(0, commentRangeStart);
            table.getContent().add(commentRangeEnd);
            table.getContent().add(createRunCommentReference(commentId));
            // ++, for next comment ...
            commentId = commentId.add(java.math.BigInteger.ONE);
         }
      }


Program runs without error but resultant docx is corrupt. error points to comment reference line if I open it in office.

Re: How to add comment to existing docx

PostPosted: Thu Jan 07, 2016 5:43 pm
by jason
Looks like you are adding your comment stuff at the table row sibling level...

A first step is to create a new docx in Word, with a table, and add a comment to it. Then inspect the underlying XML. (Save the docx and unzip it, or use the Docx4j webapp, or the Word helper add-in).

Now, compare the XML produced by your Java code, to what Word emitted.

Note that you can use the Docx4j webapp, or the Word helper add-in, to generate docx4j Java code matching the working Word example.