package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xwpf.model.XWPFCommentsDecorator;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
/**
* 获取word批注 poi3.14
* @author Administrator
*
*/
public class GetPiZhuFromWord {
private static XWPFDocument comments;
public static File getFile(String sampleFileName) {
File f = new File(sampleFileName);
return f;
}
public static InputStream openResourceAsStream(String sampleFileName) {
File f = getFile(sampleFileName);
try {
return new FileInputStream(f);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public static XWPFDocument openSampleDocument(String sampleName) throws IOException {
InputStream is = openResourceAsStream(sampleName);
return new XWPFDocument(is);
}
public static void set() throws IOException {
comments = openSampleDocument("C:\\WordWithAttachments.docx");
}
public static void main(String[] args) throws IOException{
set();
testComments();
}
public static void testComments() {
int numComments = 0;
for (XWPFParagraph p : comments.getParagraphs()) {
XWPFCommentsDecorator d = new XWPFCommentsDecorator(p, null);
if (d.getCommentText().length() > 0) {
numComments++;
System.out.println(d.getCommentText());
}
}
}
}