简体   繁体   English

使用OpenXML SDK 2.0将合并字段替换为文本

[英]Replace merge fields with text with OpenXML SDK 2.0

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? 使用openxml sdk 2.0从Word 2010文档中删除所有合并字段并将其替换为简单文本的最佳方法是什么? 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. 试图删除所有包含定义了“ MERGEFIELD”的FieldCode的Run对象,并在文本中附加了一个新的Run。 But I am missing something crucial since the field seems to stay defined for this element. 但是我缺少一些关键的东西,因为该元素的字段似乎仍然定义。

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. 我挑选出包含FieldCode的每个段落,其中包含文本“ MERGEFIELD”,并遍历每个Run,将所有其他类型推入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. 当我发现FieldChar End类型时,我回滚并将组成合并字段的四个Run替换为一个包含Text节点的Run。 The only problem I have left now is nested fields. 我现在剩下的唯一问题是嵌套字段。 Eg Merge field inside an if test. 例如,if测试中的合并字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM