docx 模板 Docx4j 替换 Java 中的文本 [英] docx Template Docx4j replacing text in Java

查看:79
本文介绍了docx 模板 Docx4j 替换 Java 中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Docx4j 的新手,我的任务是替换 docx 模板的一些文本.我阅读了 docx4j 的入门指南,但我认为我没有完全理解整个概念.好吧无论如何......我已经尝试过[Docx4j的解组模板] [1],它对给定的 docx 工作正常,但是当我自己尝试时遇到了同样的问题 模板Exceptions 表示 HashMap 不包含有效的键或值,因此它不会替换占位符.

Im new to Docx4j and my task is to replace some Text of a docx Template. I read the getting Started Guide of docx4j but I don't think I fully understood the whole concept. Well Anyway... I already tried [the unmashalling Template of Docx4j][1], which worked fine with the given docx, but then I got the same Problem when I tried it on my own template The Exceptions say, that the HashMap doesnt contain valid keys or values, and therefore it doesnt replace the placeholders.

我替换了

<w:proofErr w:type="spellEnd"/>

通过禁用拼写检查,但它仍然不起作用......而且运行该应用程序也需要相当长的时间.

by disabling the spellchecking, but it still didn't work... And it also takes quite some time to run the app.

我不明白 Getting_Started.pdf 中的数据绑定示例,所以我的选项用完了...

In didn't understand the databound example in the Getting_Started.pdf, so I'm running out of options...

如何简单地替换 docx 中的一些字符串文本?

How can I simply replace some String-Texts from a docx?

我发现如果我向 unmarshallFromTemplate.docx 添加一些文本并保存它,它不会替换新的文本行.- 标签以某种方式分成多个标签:

I found out that if I add some Text to the unmarshallFromTemplate.docx and save it, that it wont replace the new lines of text. the - Tags are somehow splitted into multiple Tags:

<w:p w:rsidR="002512F8" w:rsidRDefault="002512F8" w:rsidP="002512F8"><w:r><w:t>My</w:t></w:r><w:r w:rsidR="001A5174"><w:t xml:space="preserve"> favourite ice cream is ${DEGREE</w:t></w:r><w:r><w:t>}.</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack"/><w:bookmarkEnd w:id="0"/></w:p>

编辑 document.xml 中的文本,并添加缺少的信息并没有太大帮助.

editing the Text in the document.xml, and adding the missing Information didnt help much.

无论如何这里是我使用的 Template.docx 的 document.xml:http://uploaded.net/file/vz4qr23o

well anyway here is the document.xml of the Template.docx that im using: http://uploaded.net/file/vz4qr23o

编辑 2:

伙计们.我为自己找到了一个非常合适的解决方法,不知道为什么花了这么长时间才弄明白.正如我所说:运行分裂,我认为这是 ${} 的原因.因此,我只是在我的占位符之前使用了 # 并重写了每个占位符,以便一次运行.

Well guys. I found a quite suitable workaround for myself and dont know why it took so long to figure it out. As I was saying: The runs where splited up, and the reason for this was the ${} in my opinion. Therefore I simply used a # before my Placeholders and rewrote every placeholder, so that it would all be in one run.

不得不多次切换到 document.xml 并重写段落,但后来成功了.然后我简单地使用了一个 replace(placeholder, xml) 并替换了编组的 document.xml 的文本,然后我再次解组了它.工作.故事结束,去他妈的每晚构建或映射.谢谢

Had to switch couple of times to the document.xml and rewrite the passages but then it worked. Then I simply used a replace(placeholder, xml) and replaced the text of the marshalled document.xml, then I unmarshalled it again. Worked. End of Story, fuck the nightly build or the mappings. THX

推荐答案

docx4j 源代码已经在 GitHub 上一段时间了;该 svn 存储库已过时.

docx4j source code has been on GitHub for a while now; that svn repository is obsolete.

等效示例现在称为 变量替换.该代码效率更高一些,但您需要自己构建它,或者使用当前的每晚构建.

The equivalent sample is now called VariableReplace. That code is a bit more efficient, but you need to build it yourself, or use a current nightly build.

您可能会发现正在运行 VariablePrepare 解决了您的问题.

You'll probably find running VariablePrepare addresses your issue.

这篇关于docx 模板 Docx4j 替换 Java 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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