使用 OpenXML SDK 2.0 将合并字段替换为文本
使用 openxml sdk 2.0 从 word 2010 文档中删除所有合并字段并用简单文本替换它们的最佳方法是什么?我很难将它们清除干净。已尝试删除所有包含定义了“MERGEFIELD”的 FieldCode 的 Run 对象,并附加一个带有我的文本的新 Run 。但我遗漏了一些重要的东西,因为该字段似乎仍然为该元素定义。
What is the best way to remove all merge fields from a word 2010 document using openxml sdk 2.0, and replace them with a simple text? I have some difficulties to remove them cleanly. Have tried to remove all Run objects that includes a FieldCode with a "MERGEFIELD" defined, and appended a new Run with my text. But I am missing something crucial since the field seems to stay defined for this element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我以某种方式解决了这个问题。我挑选出包含 FieldCode(其中包含文本“MERGEFIELD”)的每个段落,并在每次运行中运行,将所有其他类型推送到列表中。当我发现 FieldChar End 类型时,我会回滚并将组成合并字段的四个 Run 替换为包含 Text 节点的单个 Run。我现在剩下的唯一问题是嵌套字段。例如,在 if 测试中合并字段。
Ok, I solved this somehow. I pick out every paragraph that contains a FieldCode that contains the text "MERGEFIELD" and run through every Run, pushing all other types onto a List. When I discover a FieldChar End type, I roll back and replace the four Runs making up the merge field with a single Run containing a Text node. The only problem I have left now is nested fields. E.g. Merge field inside an if test.