word 2007批注测试数据如下图:
代码执行结果为:
主要思路:
利用docx4j的org.docx4j.finders.CommentFinder类查找所有的批注开始和结束位置,如下图:
在遍历批注的过程中,找到批注开始位置与结束位置之间的内容,保存到Map中,key为批注的id,如下:
最后直接得到word的批注信息,打印结果时从Map中得到word正文的内容。
关键代码如下:
Parts parts = wordMLPackage.getParts(); HashMap<PartName, Part> partMap = parts.getParts(); CommentsPart commentPart = (CommentsPart) partMap .get(new CommentsPart().getPartName()); Comments comments = commentPart.getContents(); List<Comment> commentList = comments.getComment(); for (Comment comment : commentList) { StringBuffer sb = new StringBuffer(); sb.append(" ID: ").append(comment.getId()); sb.append(" 作者:").append(comment.getAuthor()); sb.append(" 时间: ").append(comment.getDate().toGregorianCalendar().getTime()); sb.append(" 内容:").append(comment.getContent()); sb.append(" 文中内容:").append(docCmtMap.get(comment.getId().toString())); System.out.println(sb.toString()); }
其他部分很简单就不写了。
本文系原创,转载请注明出处,原文链接:http://53873039oycg.iteye.com/blog/2195606 ,谢谢。
全文完。