【发布时间】:2015-01-24 16:04:50
【问题描述】:
我正在尝试用 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 完成,或者我是否遗漏了什么。
欢迎任何帮助。
【问题讨论】:
标签: html docx docx4j mergefield