原文见:http://stackoverflow.com/questions/14738446/how-to-remove-all-comments-from-docx-file-with-docx4j,稍微修改了代码。
import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.xml.bind.JAXBElement; import org.docx4j.TraversalUtil; import org.docx4j.TraversalUtil.CallbackImpl; import org.docx4j.XmlUtils; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.Part; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.Parts; import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.wml.Body; import org.docx4j.wml.CommentRangeEnd; import org.docx4j.wml.CommentRangeStart; import org.docx4j.wml.Comments; import org.docx4j.wml.Comments.Comment; import org.docx4j.wml.ContentAccessor; import org.docx4j.wml.R.CommentReference; import org.jvnet.jaxb2_commons.ppp.Child; //原文见:http://stackoverflow.com/questions/14738446/how-to-remove-all-comments-from-docx-file-with-docx4j public class Docx4j_删除所有批注_S3_Test { public static void main(String[] args) throws Exception { Docx4j_删除所有批注_S3_Test t = new Docx4j_删除所有批注_S3_Test(); t.removeAllComment("f:/saveFile/temp/sys_07_comments - 副本.docx", "f:/saveFile/temp/sys_07_comments - 副本.docx"); } //这里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)); } public boolean remove(List<Object> theList, Object bm) { // Can't just remove the object from the parent, // since in the parent, it may be wrapped in a JAXBElement for (Object ox : theList) { if (XmlUtils.unwrap(ox).equals(bm)) { return theList.remove(ox); } } return false; } static class CommentFinder extends CallbackImpl { List<Child> commentElements = new ArrayList<Child>(); public List<Object> apply(Object o) { if (o instanceof javax.xml.bind.JAXBElement && (((JAXBElement) o).getName().getLocalPart() .equals("commentReference") || ((JAXBElement) o).getName().getLocalPart() .equals("commentRangeStart") || ((JAXBElement) o) .getName().getLocalPart().equals("commentRangeEnd"))) { System.out.println(((JAXBElement) o).getName().getLocalPart()); commentElements.add((Child) XmlUtils.unwrap(o)); } else if (o instanceof CommentReference || o instanceof CommentRangeStart || o instanceof CommentRangeEnd) { System.out.println(o.getClass().getName()); commentElements.add((Child) o); } return null; } // to setParent public void walkJAXBElements(Object parent) { List children = getChildren(parent); if (children != null) { for (Object o : children) { if (o instanceof javax.xml.bind.JAXBElement && (((JAXBElement) o).getName().getLocalPart() .equals("commentReference") || ((JAXBElement) o).getName() .getLocalPart() .equals("commentRangeStart") || ((JAXBElement) o) .getName().getLocalPart() .equals("commentRangeEnd"))) { ((Child) ((JAXBElement) o).getValue()) .setParent(XmlUtils.unwrap(parent)); } else { o = XmlUtils.unwrap(o); if (o instanceof Child) { ((Child) o).setParent(XmlUtils.unwrap(parent)); } } this.apply(o); if (this.shouldTraverse(o)) { walkJAXBElements(o); } } } } } }
全文完。
相关推荐
docx4j操作word,主要使用docx4j对word进行操作,比如chart图表、标签替换、目录等。
docx4j操作word2007
最近在做一个出卷系统,需要通过试卷模板从数据库中抽取题目,然后按照模板的样式生成完整的试卷,包括试卷格式的排版和图片的写入。这是用docx4j生成word文档。里面包括完整代码和所需jar包。
使用docx4J技术操作word的读写,使用docx4J技术操作word的读写,
用docx4j,jar出去word书签,在word书签中插入文本 源码可以通过以下链接查看: https://github.com/xulp-mzl/xlp-core https://github.com/xulp-mzl/xlp-third ...实例: public static void main(String[] args) ...
这是一段用docx4j根据书签替换word文档中的内容的代码,研究了一段时间,而且下了很多东西,希望有点用
docx4j所需jar包整合,其中word转pdf,word转html,word中docx转doc,java代码实现,都可以使用,仅限学习参考使用。
word合并全家桶,包括横页和竖页兼容合并,里面包含使用文件格式转换,rtf转换为标准的word格式或者其他类型转换,jacob合并word案例及jar包,docx4j合并word案例及jar包,还有poi合并,以及Plutext-Enterprise-3.3.0.6...
docx4j解析word导入试题入库,支持图片解析,mathType公式解析,word自带数学公式解析
docx4j 用的jar包,docx4j学习网址:http://www.docx4java.org/trac/docx4j
使用docx4j相关jar包,实现多个word文档合并,并转为pdf文档格式
docx4j 用的jar包,docx4j学习网址:http://www.docx4java.org/trac/docx4j
Docx4j是Java操作office2007+中的Word、Excel、PPT的开源项目,其主要针对WordXML 同时也可以处理Excel和PPT,比POI要强大很多 . Docx4J基于开源协议ASLv2。 ASL是一个广泛适用于社区开源软件并被开源业界所认可的...
利用docx4j实现docx转pdf小dome
docx4j支持操作后缀.docx得word文档,替换书签,获取文档内容,稳定资源
代码很短,但是找jar包找的欲仙欲死,包括commons-io-1.4.jar,commons-logging-1.1.1.jar,docx4j-3.3.3.jar,freemarker.jar,google-collections.jar,log4j-1.2.8.jar,slf4j-api-1.7.12.jar,slf4j-log4j12-...
docx4j是Java操作office2007+中的Word、Excel、PPT的开源项目,其主要针对WordXML同时也可以处理Excel和PPT,比POI要强大很多(POI对Word2007支持很弱)。 资源中包含docx4j项目的jar包(及主要依赖Jar)、javadoc...
最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master最新 docx4j-master...
docx4j运行需要的jar包,这里是整理好的所有必须的jar包