如何从docx4j模板创建一个新单词 [英] how to create a new word from template with docx4j

查看:279
本文介绍了如何从docx4j模板创建一个新单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下场景,需要一些建议:

I have the following scenario, and need some advice:

用户将输入一个word文档作为模板,并在运行时提供一些参数,我的数据库和获取数据填充文档。
所以,我需要做两个基本的事情:

The user will input a word document as a template, and provide some parameters in runtime so i can query my database and get data to fill the document. So, there are two basic things i need to do:


  1. 用文档的每个键替换每个键

  2. 根据我从查询中获取的行数,将现有文档不变地复制(复制?复制?)到本身(附加),并替换键

这是最好的aprroach是什么?我现在设法做替换部分,通过使用unmarshallfromtemplate提供一个hashmap。
但是这种方式有点棘手,因为我需要在文档中添加$ {variable_name},有时单词在不同的标签中分开$ {和},导致问题。

What´s is the best aprroach to do this? I´ve managed to do the replace part for now, by using the unmarshallfromtemplate providing it a hashmap. But this way is a little bit tricky, because i need to add "${variable_name}" in the document, and sometimes word separates "${" and "}" in different tags, causing issues.

我读了关于自定义xml绑定,但是没有完全理解。我需要生成一个自定义的XML,注入它在文档(所有这个Un运行时),并调用applybindings?如果这是真的,我如何将文档中的字段绑定到xml?按名称?

I´ve read about the custom xml binding, but didn´t understand it completely. I need to generate a custom XML, inject it in the document (all of this un runtime) and call the applybindings?? If this is true, how would i bind the fields in the document to the xml ? By name?

推荐答案

docx4j包括VariablePrepare,可以整理您的输入docx,

docx4j includes VariablePrepare, which can tidy up your input docx so that your keys are not split across separate runs.

但是,您仍然最好切换到内容控制数据绑定,尤其是如果您有重复的数据(例如考虑发票中的行项目)。 披露:我在docx4j中支持此方法。

But, you would still be better off switching to content control data binding, particularly if you have repeated data (think for example of line items in an invoice). Disclosure: I champion this approach in docx4j.

采用内容控制数据绑定方法:

To adopt the content control data binding approach:


  1. 设想一个对您的数据有意义的XML格式,并编写一些代码将数据库查询的结果转换为该格式。

  1. dream up an XML format which makes sense for your data, and write some code to convert the results of your database query into that format.

修改模板,以便内容控件绑定到XML文档中的元素。通常您会使用创作Word加载项来帮助解决这个问题。 (Microsoft用于绑定的技术是XPath,因此如何绑定取决于您的XML结构,但是,您通常绑定到元素名称或ID)。

modify your template, so that the content controls are bound to elements in your XML document. ordinarily you'd use an authoring add-in for Word to help with this. (The technology Microsoft uses for binding is XPath, so how you bind depends on your XML structure, but, yes, you'd typically bind to the element name or ID).

现在你有你的XML文件和一个合适的输入docx,ContentControlsMergeXML包含你需要在运行时创建一个实例文档的代码。在 https://github.com/plutext/OpenDoPE-WAR 上还有一个servlet环境的版本。

now you have your XML file and a suitable intput docx, ContentControlsMergeXML contains the code you need to create an instance document at run time. There's also a version of this for a servlet environment at https://github.com/plutext/OpenDoPE-WAR

除了1& 2,在当前nightlies中还有org.docx4j.model.datastorage.migration.FromVariableReplacement,它可以转换你现有的$ {文档。只有标准化的目标XML格式。

As an alternative to 1 & 2, there is also org.docx4j.model.datastorage.migration.FromVariableReplacement in current nightlies, which can convert your existing "${" document. Only to a standardised target XML format though.

如果您还有其他问题,请访问http://www.docx4java.org/forums/data-binding-java-f16/

If you have further questions, there is a forum devoted to this topic at http://www.docx4java.org/forums/data-binding-java-f16/

这篇关于如何从docx4j模板创建一个新单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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