如何修改docx4j中的脚注占位符
How to modify footnote placeholder in docx4j
我有一个包含脚注的 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 时我仍然遇到问题;
输出不提取脚注参考。
我有一个包含脚注的 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 时我仍然遇到问题; 输出不提取脚注参考。