我正在尝试用 HTML 内容替换 Word 合并字段“测试”:
String myText = "<html><body><h1>Hello</h1></body></html>";
使用 Docx4j。
String myText = "<html><body><h1>Hello</h1></body></html>"; try { WordprocessingMLPackage docxOut = WordprocessingMLPackage.load(new java.io.File("/tmp/template.docx")); Map<DataFieldName, String> data = new HashMap<>(); data.put(new DataFieldName("test"), myText); org.docx4j.model.fields.merge.MailMerger.performMerge(docxOut, data, true); docxOut.save(new java.io.File("/tmp/newTemplate.docx")); } catch (Docx4JException e) { LOGGER.error(e.getMessage()); }
结果,我有一个输出(newTemplate.docx),我的合并字段替换为
"<html><body><h1>Hello</h1></body></html>"
而不被解释为 HTML。我尝试添加:
docxOut.getContentTypeManager().addDefaultContentType("html", "text/html");
但它仍然没有工作。我什至不确定在替换 Word 合并字段时解释 HTML 是否可以使用 Docx4j 完成,或者我是否遗漏了一些东西。
欢迎任何帮助。