问题描述
我是试图取代模板 DOCX
与Apache POI文件
使用 XWPFDocument
类。我在doc和 JSON
文件标签读取替换数据。我的问题是一个文本行,似乎在 DOCX以某种方式分离
当我改变其推广到邮政编码
文件打开 document.xml
。例如 [MEMBER_CONTACT_INFO]
文本成为 [MEMBER_CONTACT_INFO
和]
分别。 POI
在自 DOCX
原来是这样以同样的方式读取此。这将在第2款 XWPFRun
对象这显示文本为 [MEMBER_CONTACT_INFO
和]
分开。
I 'm trying to replace a template DOCX
document with Apache POI
by using the XWPFDocument
class. I have tags in the doc and a JSON
file to read the replacement data. My problem is that a text line seems separated in a certain way in DOCX
when I change its extension to ZIP
file and open document.xml
. For example [MEMBER_CONTACT_INFO]
text becomes [MEMBER_CONTACT_INFO
and ]
separately. POI
reads this in the same way since the DOCX
original is like this. This creates 2 XWPFRun
objects in the paragraph which show the text as [MEMBER_CONTACT_INFO
and ]
separately.
我的问题是,有没有办法迫使 POI
通过合并相关的运行或类似的东西,如Word运行?或者,我怎么能解决这个问题?我正在运行的匹配,而文本替换因为它被分成2个不同的运行对象我找不到我的标签。
My question is, is there a way to force POI
to run like Word via merging related runs or something like that? Or how can I solve this problem? I 'm matching run texts while replacing and I can't find my tag because it is split into 2 different run object.
最佳
推荐答案
这浪费了那么多我的时间...一次
This wasted so much of my time once...
基本上,一个 XWPFParagraph
是由多个的 XWPFRun
s和XWPFRun是具有固定的传染性文本同样的风格。
Basically, an XWPFParagraph
is composed of multiple XWPFRun
s, and XWPFRun is a contagious text that has a fixed same style.
所以,当你尝试写一些像[PLACEHOLDER_NAME]在MS-Word中它会创建一个单一的XWPFRun。但是,如果你不小心添加了一些东西多了,然后你回去改[PLACEHOLDER_NAME]以别的东西它永远不会保证它仍将是一个 XWPFRun
它很可能,这将拆分到两分。据我所知,这是MS-Word如何工作的。
So when you try writing something like "[PLACEHOLDER_NAME]" in MS-Word it will create a single XWPFRun. But if you somehow add a few things more, and then you go back and change "[PLACEHOLDER_NAME]" to something else it is never guaranteed that it will remain a single XWPFRun
it is quite possible that it will split to two Runs. AFAIK this is how MS-Word works.
如何避免奔跑的分裂在这种情况下?
解决方案:有两种解决方案,我所知道的:
Solution: There are two solutions that I know of:
-
复制文本[PLACEHOLDER_NAME]到记事本什么的。让你的必要的修改和复制回来,并粘贴,而不是[PLACEHOLDER_NAME]你的字的文件,这样一来你的整个[PLACEHOLDER_NAME]将被新的文本,避免XWPFRuns分裂取代。
Copy text "[PLACEHOLDER_NAME]" to Notepad or something. Make your necessary modification and copy it back and paste it instead of "[PLACEHOLDER_NAME]" in your word file, this way your whole "[PLACEHOLDER_NAME]" will be replaced with new text avoiding splitting of XWPFRuns.
选择[PLACEHOLDER_NAME],然后单击MS-Word中的替换选项,并替换为[您的全新编辑的占位符],这将保证您新的占位符会消耗一个XWPFRun
Select "[PLACEHOLDER_NAME]" and then click of MS-Word "Replace" option and Replace with "[Your-new-edited-placeholder]" and this will guarantee that your new placeholder will consume a single XWPFRun.
如果您需要再次更改新的占位符,按照步骤1或2。
If you have to change your new placeholder again, follow step 1 or 2.
这篇关于在Apache的POI XWPFRun对象分隔文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!