Akos Lukacs
Akos Lukacs

Reputation: 2047

Hiding parts of OpenXml documents programmatically

Is it possible to programmatically hide parts of an OpenXML document, without actually removing it?

The reason I would want to do this: This is a template file, dynamic parts are filled using databindig. And some parts should be hidden, if there is no backing data. But don't want to actually remove parts from the document, so the document could be "refreshed" later with new data.

Something like display: none in html/css.

Upvotes: 1

Views: 2024

Answers (1)

Edward Wilde
Edward Wilde

Reputation: 26517

The is no exact equivalent to hiding content in Word using the open xml sdk. However there are two approaches that might work for you:

Hidden paragraph trick

Create a style, let's call it HiddenParagraph. Define it in your styles.xml as follows:

 <w:style w:type="paragraph" w:customStyle="1" w:styleId="HiddenParagraph">
    <w:name w:val="HiddenParagraph" />
    <w:next w:val="Normal" />
    <w:pPr>
    <w:spacing w:line="14" w:lineRule="auto" />
    </w:pPr>
    <w:rPr>
      <w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi" />
      <w:sz w:val="22" />
      <w:szCs w:val="22" />
    </w:rPr>
 </w:style>

The w:line=14 makes the paragraph effectively invisible.

Now render the content your don't want to see using this paragraph style.

 <w:p>
  <w:pPr>
    <w:pStyle w:val="HiddenParagraph" />
  </w:pPr>
  <w:r>
    <w:text>you should not be able to see me
  </w:r>
</w:p>

To show the content again just change the paragraph style to normal or some other more sane style.

Custom XML Part

Store data you don't want to display in the document in a custom xml data store, although this might not work in your specific scenario

Reference http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2010/10/27/59361.aspx

Upvotes: 3

Related Questions