java - 用文本替换内容控件
解决方案
private static void replaceTextValue(WordprocessingMLPackage template, String name, String placeholder)
throws Exception {
List<Object> texts = getAllElementFromObject(template.getMainDocumentPart(), SdtBlock.class);
// System.out.println(template.getMainDocumentPart().getXML());
for (Object text : texts) {
SdtBlock textElement = (SdtBlock) text;
List<Object> cList = textElement.getSdtContent().getContent();
ClassFinder finder = new ClassFinder(Text.class);
new TraversalUtil(cList, finder);
for (Object o : finder.results) {
Object o2 = XmlUtils.unwrap(o);
if (o2 instanceof org.docx4j.wml.Text) {
String CTagVal = ((org.docx4j.wml.Text) o2).getValue();
if (CTagVal.equalsIgnoreCase(placeholder)) {
org.docx4j.wml.Text txt = (org.docx4j.wml.Text) o2;
txt.setValue(name);
}
} else {
System.out.println(XmlUtils.marshaltoString(o, true, true));
}
}
}
}
此方法替换控件内的文本
推荐阅读
- java - 错误:无法为测试用例 5595c7f4-64d1-494e-b388-be47a3ca9947 设置浏览器通信
- git - 文件名中的变音符号导致颠覆和 git (MacOS) 之间的差异
- sql - sql跟踪更改的值
- reactjs - React - 基于 API 响应渲染动态组件
- javascript - 删除多维数组中的重复项
- java - 使用全局异常处理程序在 SpringBoot 应用程序中处理 BindException
- visual-studio-code - 在 VS Code 上的 Asp.net Core 应用程序中集成 Devexpress 工具
- elixir - 如何按键查询json对象列表
- python - 如何确保matplotlib图x轴上标签之间的间距均匀?
- python - 使用 pyodbc 的 SQL 查询,其中所选数据位于数据框中