我在使用Apache POI 3.15从docx文件中读取空格时遇到问题 . 我有一个带有换行符的Word文档,当通过apache poi读取文件时,我无法找到获取这些换行符的方法 . 当我调用 paragraph.getParagraphText()
时,返回带有换行符的文本 . 当我遍历XWPFRun对象时,我只获得文本和格式,但没有关于换行符的信息 .
这是我使用的代码 . br,tab,cr和separator列表始终为空 .
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for(XWPFParagraph paragraph : paragraphs) {
//System.out.println(paragraph.getParagraphText());
for(XWPFRun run : paragraph.getRuns()) {
CTR ctr = run.getCTR();
List<CTBr> brList = ctr.getBrList();
List<CTEmpty> tabList = ctr.getTabList();
List<CTEmpty> crList = ctr.getCrList();
List<CTEmpty> separatorList = ctr.getSeparatorList();
String text = run.getText(run.getTextPosition());
String color =run.getColor();
boolean bold = run.isBold();
boolean italic = run.isItalic();
System.out.println("text: " + text + " color: " + color + " bold: " + bold + " italic: " + italic);
for(CTEmpty cr : crList) {
System.out.println(cr);
}
}
}
是使用CTR对象来纠正方式还是有另一种方法来获取这些换行符?
1 回答
我找到了一个解决方案来获得换行符 . 正常输入作为自己的段落返回,没有带有spacingAfter值的文本 . 段落中的软输入通过
run.getCTR().getBrList
作为中断返回