use of org.docx4j.wml.Comments in project docx4j-template by vindell.
the class Docx4j_创建批注_S3_Test method addDocumentCommentsPart.
public Comments addDocumentCommentsPart(WordprocessingMLPackage wordMLPackage, ObjectFactory factory) throws Exception {
CommentsPart cp = new CommentsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(cp);
Comments comments = factory.createComments();
cp.setJaxbElement(comments);
return comments;
}
use of org.docx4j.wml.Comments in project docx4j-template by vindell.
the class Docx4j_创建批注_S3_Test method testCreateComment.
public void testCreateComment(WordprocessingMLPackage wordMLPackage, MainDocumentPart t, ObjectFactory factory) throws Exception {
P p = factory.createP();
setParagraphSpacing(factory, p, true, "0", "0", true, null, "100", true, "240", STLineSpacingRule.AUTO);
t.addObject(p);
RPr fontRPr = getRPrStyle(factory, "微软雅黑", "000000", "20", STHint.EAST_ASIA, false, false, false, true, UnderlineEnumeration.SINGLE, "B61CD2", true, "darkYellow", false, null, null, null);
RPr commentRPr = getRPrStyle(factory, "微软雅黑", "41A62D", "18", STHint.EAST_ASIA, true, true, false, false, null, null, false, null, false, null, null, null);
Comments comments = addDocumentCommentsPart(wordMLPackage, factory);
BigInteger commentId = BigInteger.valueOf(1);
createCommentEnd(factory, p, "测试", "这是官网Demo", fontRPr, commentRPr, commentId, comments);
commentId = commentId.add(BigInteger.ONE);
createCommentRound(factory, p, "批注", "这是批注comment", fontRPr, commentRPr, commentId, comments);
commentId = commentId.add(BigInteger.ONE);
p = factory.createP();
setParagraphSpacing(factory, p, true, "0", "0", true, null, "100", true, "240", STLineSpacingRule.AUTO);
t.addObject(p);
createCommentRound(factory, p, "批注2", "这是批注comment2", fontRPr, commentRPr, commentId, comments);
commentId = commentId.add(BigInteger.ONE);
createCommentEnd(factory, p, "测试2", "这是官网Demo", fontRPr, commentRPr, commentId, comments);
commentId = commentId.add(BigInteger.ONE);
}
use of org.docx4j.wml.Comments in project docx4j-template by vindell.
the class Docx4j_工具类_S3_Test method getComments.
public int getComments(WordprocessingMLPackage wordMLPackage) throws Exception {
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());
}
return 0;
}
use of org.docx4j.wml.Comments in project docx4j-template by vindell.
the class Docx4j_删除所有批注_S3_Test method removeAllComment.
// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
// 清空comments.xml内容
Parts parts = wordMLPackage.getParts();
HashMap<PartName, Part> partMap = parts.getParts();
CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
Comments comments = commentPart.getContents();
List<Comment> commentList = comments.getComment();
for (int i = 0, len = commentList.size(); i < len; i++) {
commentList.remove(0);
}
// 清空document.xml文件中批注
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
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);
}
wordMLPackage.save(new FileOutputStream(savePath));
}
Aggregations