使用 Open XML SDK 替换 Word 文件中的书签文本
我认为 v2.0 更好...他们有一些不错的“如何:...” 示例,但书签的作用似乎并不像表格那样明显...书签是由两个 XML 元素BookmarkStart & BookmarkEnd。我们有一些模板,其中的文本作为书签,我们只是想用其他文本替换书签...没有奇怪的格式发生,但如何选择/替换书签文本?
I assume v2.0 is better... they have some nice "how to:..." examples but bookmarks don't seem to act as obviously as say a Table... a bookmark is defined by two XML elements BookmarkStart & BookmarkEnd. We have some templates with text in as bookmarks and we simply want to replace bookmarks with some other text... no weird formatting is going on but how do I select/replace bookmark text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这是我在受到你们的启发后采取的方法:
Here's my approach after using you guys as inspiration:
用单个内容(可能是多个文本块)替换书签。
首先,删除开始和结束之间的现有内容。然后,新的运行将直接添加到开始后面(结束之前)。
但是,不确定书签在打开时是否在另一个部分或在不同的表格单元格等中关闭。
对我来说,现在就足够了。
Replace bookmarks with a single content (possibly multiple text blocks).
First, the existing content between start and end is removed. Then a new run is added directly behind the start (before the end).
However, not sure if the bookmark is closed in another section when it was opened or in different table cells, etc. ..
For me it's sufficient for now.
经过很多小时,我编写了这个方法:
之后,我调用:
如果您没有空书签,则此代码运行良好。
我希望它可以帮助别人。
After a lot of hours, I have written this method:
After that, I call:
This code is working good if u have no empty Bookmarks.
I hope it can help someone.
我十分钟前才弄清楚这一点,所以请原谅代码的黑客性质。
首先,我编写了一个辅助递归辅助函数来查找所有书签:
这会返回一个字典,我可以用它来划分替换列表并在书签后添加文本:
据我所知,插入和替换书签看起来更困难。当我使用 InsertAt 而不是 InsertIntoSelf 时,我得到:“非复合元素没有子元素。”青年MMV
I just figured this out 10 minutes ago so forgive the hackish nature of the code.
First I wrote a helper recursive helper function to find all the bookmarks:
That returns me a Dictionary that I can use to part through my replacement list and add the text after the bookmark:
From what I can tell inserting into and replacing the bookmarks looks harder. When I used InsertAt instead of InsertIntoSelf I got: "Non-composite elements do not have child elements." YMMV
我从答案中获取了代码,并且在特殊情况下遇到了几个问题:
bookmarkStart.Parent.InsertAfter(new Run(new Text("Hello World")), bookmarkStart)
您可以在这里查看我的具体实现)
希望这可以帮助那些遇到同样问题的人。
I took the code from the answer, and had several problems with it for exceptional cases:
bookmarkStart.Parent.InsertAfter(new Run(new Text("Hello World")), bookmarkStart)
You can view my specific implementation here)
Hope this helps some of you who experienced the same issues.
这里的大多数解决方案都假设在运行之前开始并在运行之后结束的常规书签模式,但这并不总是正确的,例如,如果书签在一个段落或表中开始并在另一个段落中的某个地方结束(就像其他人指出的那样)。如何使用文档顺序来处理书签未放置在常规结构中的情况 - 文档顺序仍然会找到其间的所有相关文本节点,然后可以将其替换。只需执行 root.DescendantNodes().Where(xtext or bookmarkstart or bookmark end) ,它将按文档顺序遍历,然后可以替换在看到书签开始节点之后但在看到结束节点之前出现的文本节点。
Most solutions here assume a regular bookmarking pattern of starting before and ending after runs, which is not always true e.g. if bookmark starts in a para or table and ends somewhere in another para (like others have noted). How about using document order to cope with the case where bookmarks are not placed in a regular structure - the document order will still find all the relevant text nodes in between which can then be replaced. Just do root.DescendantNodes().Where(xtext or bookmarkstart or bookmark end) which will traverse in document order, then one can replace text nodes that appear after seeing a bookmark start node but before seeing an end node.
以下是我如何使用 VB 在 bookmarkStart 和 BookmarkEnd 之间添加/替换文本。
Here is how i do it and VB to add/replace text between bookmarkStart and BookmarkEnd.
我需要用表格替换书签的文本(书签名称为“表格”)。这是我的方法:
希望这有帮助
I needed to replace the text of a bookmark (bookmarks name is "Table") with a table. This is my approach:
Hope this helps
下面是我在 VB.NET 中的操作方法:
Here is how I do it in VB.NET:
接受的答案和其他一些答案对书签在文档结构中的位置做出了假设。这是我的 C# 代码,它可以处理替换跨越多个段落的书签,并正确替换不在段落边界开始和结束的书签。仍然不完美,但更接近......希望它有用。如果您找到更多改进方法,请编辑!
The accepted answer and some of the others make assumptions about where the bookmarks are in the document structure. Here's my C# code, which can deal with replacing bookmarks that stretch across multiple paragraphs and correctly replace bookmarks that do not start and end at paragraph boundaries. Still not perfect, but closer... hope it's useful. Edit if you find more ways to improve it!
这就是我最终得到的结果 - 不是 100% 完美,但适用于简单的书签和简单的文本插入:
Here's what I ended up with - not 100% perfect but works for simple bookmarks and simple text to insert:
基于 cyberblast 的答案,这里有一个简单的修改,它保留书签内第一个元素的属性(样式),如果它是运行:
如果书签中的第一件事不是运行,我会从书签之前最接近的运行获取属性,因为这适合我的情况。您可能想要其他东西,例如在书签内更深入地搜索样式。
Building on cyberblast’s answer, here is a simple modification that retains the properties (styling) of the first element inside the Bookmark if it is a Run:
If the first thing in the Bookmark is not a Run, I take the properties from the closest Run before the bookmark, because that’s what works for my case. You may want to something else, like search for styling deeper inside the bookmark.