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.