c# - 使用自定义 XML 部分隐藏 Word 内容控件

标签 c# ms-word ms-office openxml openxml-sdk

我有一个带有一堆内容控件的 word 文档。这些被映射到自定义 XML 部分。为了即时构建文档,我只需覆盖自定义 XML 部分。

我遇到的问题是,如果我没有定义特定项目,它的空间在文档中仍然可见,将其下方的内容向下推,看起来与文档的其余部分不一致。

这是我的代码的一个基本示例:

var path = HttpContext.Current.Server.MapPath("~/Classes/Word/LawyerBio.docx");
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(path, true))
{
      //create new XML string
      //these values will populate the template word doc
      string newXML = "<root>";

      if (!String.IsNullOrEmpty(_lawyer["Recognition"]))
      {
          newXML += "<recognition>";
          newXML += _text.Field("Recognition Title");
          newXML += "</recognition>";
       }
       if (!String.IsNullOrEmpty(_lawyer["Board Memberships"]))
       {
          newXML += "<boards>";
          newXML += _text.Field("Board Memberships Title");
          newXML += "</boards>";
       }
       newXML += "</root>";
       MainDocumentPart mainPart = myDoc.MainDocumentPart;

       //delete old xml part
       mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);

       //add new xml part
       CustomXmlPart customXml = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
       using(StreamWriter ts = new StreamWriter(customXml.GetStream()))
       {
           ts.Write(newXML);
       }
       myDoc.Close();
}

有没有办法让这些内容控件真正折叠/隐藏?

最佳答案

我认为您必须在 Word 中打开 docx 之前进行一些预处理,或者进行一些后处理(例如通过宏)。

作为预处理方法的一个例子,OpenDoPE定义一个“条件”,您可以使用它来排除未定义的内容。

关于c# - 使用自定义 XML 部分隐藏 Word 内容控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749501/

相关文章:

c# - C# 中 XML List<T> 反序列化的问题

java - 使用来自 java 对象的值从模板动态创建 word 文档

javascript - 如何在独立脚本中使用 Microsoft Office 2013 javascript API

excel - 如果 MS excel 中的单元格值为空或为零,则创建一个删除特定单元格并向上移动行的按钮

'new'方法中的C#递归

c# - 使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?

c# - 正则表达式 : language generator

c# - 如何在没有 Office.Interop 的情况下读取 'Extended' MS Word 文件标签?

java - 使用java更新word文档中自定义属性的值

excel - 通过工作表上设计的自定义按钮,使用宏 vba 将一个 xls 的所有工作表复制到另一个 xls 文件