I am inserting XML in place of a [token] in a word document. This XML is simple. I have header and footer in my application and when I insert this XML, the header and footer disappear.
I've renamed the .docx to .zip and I navigated to the header xml and I can see that it is still present but it does not show.
The XML that I am inserting into the wordprocessingdocument is below. I do not know much about XML but the content inserting shows up in the document. I cannot figure out why it is causing my header content to disappear. I can still see the header and footer sections and I've checked all my options so it isn't an option in the word document that I know of.
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:spacing w:before="0" w:after="0" w:afterAutospacing="0" w:line="240" />
<w:rPr />
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" />
<w:sz w:val="24" />
</w:rPr>
<w:t xml:space="preserve">This is a test</w:t>
</w:r>
</w:p>
<w:sectPr><w:type w:val="nextPage" />
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1425" w:right="1425" w:bottom="1425" w:left="1425" />
</w:sectPr>
</w:body>
Any help would be appreciated. I'll reply if I can provide additional information.
The issue you are facing is likely related to the insertion of the <w:sectPr> element in the XML. When you insert the <w:sectPr> tag, it defines section properties, which can inadvertently override existing header and footer settings. Word documents manage headers and footers at the section level, so inserting new section properties without specifying headers and footers can result in the disappearance of these elements.
To resolve this, you should explicitly include the header and footer references within the section properties (<w:sectPr>) to preserve them. Here’s an example of how to modify your XML to include header and footer references:
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p>
<w:pPr>
<w:spacing w:before="0" w:after="0" w:afterAutospacing="0" w:line="240" />
<w:rPr />
</w:pPr>
<w:r>
<w:rPr>
<w:rFonts w:ascii="Times New Roman" />
<w:sz w:val="24" />
</w:rPr>
<w:t xml:space="preserve">This is a test</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="nextPage" />
<w:pgSz w:w="12240" w:h="15840" />
<w:pgMar w:top="1425" w:right="1425" w:bottom="1425" w:left="1425" />
<!-- Include references to headers and footers -->
<w:headerReference w:type="default" r:id="rIdHeader"/>
<w:footerReference w:type="default" r:id="rIdFooter"/>
</w:sectPr>
</w:body>