开发者

Hiding Word Content Control With Custom XML Parts

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜