需要在一个word文档中,找到某个批注(comment),然后删除这个comment, 网上搜了好久,只能找到读取comment的实例,于是只能自己摸索,终于找到办法了,不敢私藏,分享给需要的人。方法不太好,是先找到Comment所在的Paragraph,再把Paragraph删除,重建这个Paragraph。如果哪位高手有更好的方法,欢迎指教。
void removeComment(XWPFTableCell cell) {
XWPFComment coment;
for (int i=0; i< cell.getParagraphs().size(); i++) {
XWPFParagraph paragraph = cell.getParagraphs().get(i);
//逐段查找Comment
for (CTMarkupRange anchor : paragraph.getCTP().getCommentRangeStartList()) {
if ((coment = paragraph.getDocument().getCommentByID(anchor.getId().toString())) != null) {
log.debug("comment text is: {}", coment.getText());
//找到了comment
String text = paragraph.getText();
//先把comment所在的Paragraph删除
cell.removeParagraph(i);
//再重建Paragraph
cell.addParagraph().createRun().setText(text);
}
}
}
}