问题: 需要实现一个替换 docx 内文本的功能,但是目前 docx 中文本框的内容读取不到
开发环境
- springboot 2.x
- poi 5.2.2 poi-ooxml 5.2.2
实现代码
public void changeText(XWPFDocument document, Map<String, String> textMap) {
// 替换页眉
for (XWPFHeader xwpfHeader : document.getHeaderList()) {
for (XWPFParagraph paragraph : xwpfHeader.getParagraphs()) {
replaceParagraph(paragraph, textMap);
}
}
// 替换页脚
for (XWPFFooter xwpfFooter : document.getFooterList()) {
for (XWPFParagraph paragraph : xwpfFooter.getParagraphs()) {
replaceParagraph(paragraph, textMap);
}
}
document.getBodyElements();
// 替换段落内的文本信息
List<XWPFParagraph> documentParagraphs = document.getParagraphs();
for (XWPFParagraph xwpfParagraph : documentParagraphs) {
replaceParagraph(xwpfParagraph, textMap);
}
.....(省略)
其中 document.getParagraphs() 应该能读取 word 中所有段落的数据,但是很遗憾没有找到任何文本框中的段落或文本。我也没找到相关单独读取文本框对象的方法。
idea 断点调试把 XWPFDocument 对象中的数据翻了一个边也只找到普通的不在文本框里的文本。
现在有点懵,不知道怎么处理了。