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

发布于 2024-11-25 05:13:39 字数 1400 浏览 3 评论 0原文

我有一个 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();
}

有什么方法可以使这些内容控件实际上折叠/隐藏吗?

I have a word document with a bunch of content controls on it. These are mapped to a custom XML part. To build the document on the fly, I simply overwrite the custom XML part.

The problem I'm having, is that if I don't define a particular item, it's space is still visible in the document, pushing down the stuff below it, and looking inconsistent with the rest of the document.

Here's a basic example of my code:

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();
}

Is there any way to make these content controls actually collapse/hide?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-02 05:13:40

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

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

I think you will have to do either some preprocessing before the docx is opened in Word, or some postprocessing (eg via a macro).

As an example of the preprocessing approach, OpenDoPE defines a "condition" which you could use to exclude the undefined stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文