docx4j 用 html 替换变量 [英] docx4j replace variable with html

查看:56
本文介绍了docx4j 用 html 替换变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了用文本替换变量的示例代码,效果很好.

i got this sample code to replace variables with text and it works perfect.

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("c:/template.docx"));

VariablePrepare.prepare(wordMLPackage);

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();            

HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("firstname", "Name"); //${firstname}
mappings.put("lastname", "Name"); //${lastname}

documentPart.variableReplace(mappings);

wordMLPackage.save(new java.io.File("c:/replace.docx"));

但现在我必须用 html 替换变量.我试过这样的事情.但因为它不起作用

but now i have to replace the variables with html. I tried something like this. but of cause it does not work

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("c:/template.docx"));

VariablePrepare.prepare(wordMLPackage);

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();    

String html = "<html><head><title>Import me</title></head><body><p style='color:#ff0000;'>Hello World!</p></body></html>"; 

AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
afiPart.setBinaryData(html.toString().getBytes());
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = documentPart.addTargetPart(afiPart);
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());


HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("firstname", ac.toString()); //${firstname}
mappings.put("lastname", "Name"); //${lastname}

documentPart.variableReplace(mappings);

wordMLPackage.save(new java.io.File("c:/replace.docx"));

有什么办法可以实现吗?

Is there any way to achieve this?

推荐答案

变量替换的东西都是关于替换 WordML 中的简单值,它不适用于 HTML.

The variable replacement stuff is all about swapping out simple values in WordML, it won't work for HTML.

您需要以正确的方式将 (X)HTML 导入 Word 文档.在最新版本的 docx4j 中,这是通过 ImportXHTML 子项目完成的:https://github.com/plutext/docx4j-ImportXHTML(在早期版本中,XHTML 导入代码是主要 docx4j 项目的一部分).

You need to import (X)HTML into your Word document the correct way. In the latest version of docx4j, this is done via the ImportXHTML sub-project: https://github.com/plutext/docx4j-ImportXHTML (in earlier versions, the XHTML import code is part of the main docx4j project).

本质上,代码采用格式良好的 XHTML,将其解析为 WordML 结构(即文本元素、运行、段落等),然后您可以将生成的对象集合插入到您的 Word 文件中.一个例子:

Essentially the code takes well-formed XHTML, parses it into WordML constructs (i.e. text elements, runs, paragraphs and so on), and then you can insert the resulting collection of objects into your Word file. An example:

// Where xhtml = String representing well-formed XHTML to insert
// Where pkg = your WordProcessingMLPackage instance
XHTMLImporterImpl importer = new XHTMLImporterImpl(pkg);
pkg.getMainDocumentPart().getContent().addAll(importer.convert(xhtml, null));

这篇关于docx4j 用 html 替换变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆