本文介绍了Apache POI XWPFRun 对象中的分隔文本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XWPFDocument 类用 Apache POI 替换模板 DOCX 文档.我在文档中有标签和一个 JSON 文件来读取替换数据.我的问题是当我将其扩展名更改为 ZIP 文件并打开 document.xml 时,文本行在 DOCX 中似乎以某种方式分开.例如 [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组成,而XWPFRun是一个具有固定相同风格的传染性文本.

Basically, an XWPFParagraph is composed of multiple XWPFRuns, and XWPFRun is a contagious text that has a fixed same style.

因此,当您尝试在 MS-Word 中编写诸如[PLACEHOLDER_NAME]"之类的内容时,它将创建一个 XWPFRun.但是,如果您以某种方式添加更多内容,然后返回并将[PLACEHOLDER_NAME]"更改为其他内容,则永远无法保证它会保持单个 XWPFRun 很可能它会拆分为两个运行.AFAIK 这就是 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:

  1. 将文本[PLACEHOLDER_NAME]"复制到记事本或其他东西.进行必要的修改并将其复制回并粘贴到您的 word 文件中,而不是[PLACEHOLDER_NAME]",这样您的整个[PLACEHOLDER_NAME]"将被替换为新文本,避免拆分 XWPFRuns.

  1. 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替换"选项并替换为[Your-new-edited-placeholder]",这将保证您的新占位符将使用单个 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 对象中的分隔文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:50