我有一个包含脚注的 docx 文件。我在脚注文本中有一个需要替换的占位符。在提取节点并修改占位符未通过的文本值时。出于某种原因,我认为它没有使用脚注中提供的文本。
你能指导我如何在脚注中替换占位符吗?
方法一
如果您还没有导致解组发生,则更快:
FootnotesPart fp = wordMLPackage.getMainDocumentPart().getFootnotesPart();
fp.variableReplace(mappings);
方法二
FootnotesPart fp = wordMLPackage.getMainDocumentPart().getFootnotesPart();
// unmarshallFromTemplate requires string input
String xml = XmlUtils.marshaltoString(fp.getJaxbElement(), true);
// Do it...
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
// Inject result into docx
fp.setJaxbElement((CTFootnotes) obj);
由于@JasonPlutext 的回答不适用于我的情况,因此我发布了对我有用的内容
FootnotesPart fp = template.getMainDocumentPart().getFootnotesPart();
List<Object> texts = fp.getJAXBNodesViaXPath("//w:t", true);
for(Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
// do variable replacement
text.setValue(textValue);
}
但是在使用 Docx4J.toPDF(..); 将其导出为 pdf 时,我仍然面临这个问题;输出不选取脚注参考。