先上代码
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
代码
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
/**
* 删除批注
*/
public static void main(String[] args) throws Exception{
final XWPFDocument xwpfComment
= new XWPFDocument(new FileInputStream("文件路径"));
//删除段落中得批注
final List<XWPFParagraph> paragraphs = xwpfComment.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
final List<XWPFRun> runs = paragraph.getRuns();
for(XWPFRun run : runs ) {
final CTR ctr = run.getCTR();
try {
//这里写的比较暴力,不管有没有批注都删除,其实是可以加判断的
ctr.removeCommentReference(0);
ctr.removeCommentReference(1);
ctr.removeCommentReference(2);
} catch (Exception e) {
}
}
}
final FileOutputStream outputStream = new FileOutputStream("新文件路径");
xwpfComment.write(outputStream);
xwpfComment.close();
}