2
votes

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?

1
I'm running into this as well now. Do you have any insight into the best way to accomplish this? With 2013, I'm hoping to make it work with Repeating Content Controls, but it's not very intuitive and feels like a hack.JoeBrockhaus
@JoeBrockhaus It's been a few years, but I seem to remember the content control having an option on it to make it invisible. I think this was set through the Word layout, not through code. Hopefully that leads you down the right path.Cory Dee

1 Answers

0
votes

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.