问题描述
由于这本很棒的指南.
但是现在我正在尝试做一些更复杂的事情.
But now I'm trying to do something more complicated.
我想做的是在文档中找到我的标记文本#1,在文档中找到我的标记文本#2,然后在两者之间复制所有内容.然后,我将对该内容粘贴X次,然后进行进一步的更改.
What I'd like to do is find my marker text #1 within the document, find my marker text #2 within the document, and copy EVERYTHING inbetween the two. I will then be pasting that content X number of times and doing further alterations.
有人知道我将如何做到这一点,并可能将我指向所需的关键功能吗?
Does anyone know how I would do this, and possibly point me to the key functions needed?
推荐答案
在一般情况下,这并不是一件容易的事,因为两个标记之间可能存在多种结构,需要特殊处理(例如图像,脚注,sectPr元素,书签等).关于一般情况,请参见我在MergeDocx上的博客文章.
In the general case, that's not a simple thing to do, because there could be a variety of structures between your two markers which demand special handling (think images, footnotes, sectPr elements, bookmarks etc). Regarding that general case, see my blog post on MergeDocx.
但是,如果您可以做一些简化的假设,那么它将变得更加容易.
However, if you can make some simplifying assumptions, then it becomes easier.
首先,假设您的标记是块级元素.
First, assume your markers are block level elements.
第二,假设您的文档只是格式化的文本和表格.
Second, assume your document is just formatted text and tables.
然后,您可以仅在块级内容列表上执行操作:
Then you can just perform operations on the list of block level content:
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
List<Object> blocks = documentPart.getContent();
有XmlUtils.deepCopy可以根据需要克隆对象.
There is XmlUtils.deepCopy to clone objects as necessary.
对于每个违反假设2的结构,您都需要进行特定的处理.如果您可以控制输入文档,则可以进行管理.
For each structure which contravenes assumption 2, you'll need specific handling. If you have control over your input documents, you'll be able to manage this.
作为标记#1和#2的替代方法(类似于使用书签),请考虑使用块级内容控件.这样可以避免脆性点标签;从XML的角度来看,它更好,并且在Word用户界面中(从创作的角度来看)具有优势.
As an alternative to marker #1 and #2, which is similar to using bookmarks, consider using a block level content control. This avoids brittle point tags; it is nicer from an XML point of view, and offer advantages in the Word user interface (from an authoring point of view).
这篇关于DOCX4J-在两个标记之间复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!