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

查看:36
本文介绍了如何使用 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. 合并"(复制?重复?)根据我从查询中获得的行数,将现有文档更改为自身(追加),并将新副本中的键替换为下一行值.

执行此操作的最佳方法是什么?我现在设法通过使用 unmarshallfromtemplate 为其提供哈希图来完成替换部分.但这种方式有点棘手,因为我需要在文档中添加${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,将它注入到文档中(所有这些都不是运行时)并调用 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-WARp>

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、current 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天全站免登陆