/// <summary> /// Set a new header in a document /// </summary> /// <param name="header">XDocument containing the header to add in the document</param> /// <param name="type">The header part type</param> public static OpenXmlPowerToolsDocument SetHeader(WmlDocument doc, XDocument header, HeaderType type, int sectionIndex) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { // Removes the reference in the document.xml and the header part if those already // exist XElement headerReferenceElement = GetHeaderReference(document, type, sectionIndex); if (headerReferenceElement != null) { GetHeaderPart(document, type, sectionIndex).RemovePart(); headerReferenceElement.Remove(); } // Add the new header HeaderPart headerPart = document.MainDocumentPart.AddNewPart <HeaderPart>(); headerPart.PutXDocument(header); // Creates the relationship of the header inside the section properties in the document string relID = document.MainDocumentPart.GetIdOfPart(headerPart); AddHeaderReference(document, type, relID, sectionIndex); // add in the settings part the EvendAndOddHeaders. this element // allow to see the odd and even headers and headers in the document. SettingAccessor.AddEvenAndOddHeadersElement(document); } return(streamDoc.GetModifiedDocument()); } }
/// <summary> /// Set a new footer in a document /// </summary> /// <param name="footer">XDocument containing the footer to add in the document</param> /// <param name="type">The footer part type</param> public static OpenXmlPowerToolsDocument SetFooter(WmlDocument doc, XDocument footer, FooterType type, int sectionIndex) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { // Removes the reference in the document.xml and the footer part if those already // exist XElement footerReferenceElement = GetFooterReference(document, type, sectionIndex); if (footerReferenceElement != null) { GetFooterPart(document, type, sectionIndex).RemovePart(); footerReferenceElement.Remove(); } // Add the new footer FooterPart footerPart = document.MainDocumentPart.AddNewPart <FooterPart>(); footerPart.PutXDocument(footer); // If the document does not have a property section a new one must be created if (SectionPropertiesElements(document).Count() == 0) { AddDefaultSectionProperties(document); } // Creates the relationship of the footer inside the section properties in the document string relID = document.MainDocumentPart.GetIdOfPart(footerPart); string kindName = ""; switch ((FooterType)type) { case FooterType.First: kindName = "first"; break; case FooterType.Even: kindName = "even"; break; case FooterType.Default: kindName = "default"; break; } XElement sectionPropertyElement = SectionPropertiesElements(document).Skip(sectionIndex).FirstOrDefault(); if (sectionPropertyElement != null) { sectionPropertyElement.Add( new XElement(ns + "footerReference", new XAttribute(ns + "type", kindName), new XAttribute(relationshipns + "id", relID))); if (sectionPropertyElement.Element(ns + "titlePg") == null) { sectionPropertyElement.Add( new XElement(ns + "titlePg") ); } } document.MainDocumentPart.PutXDocument(); // add in the settings part the EvendAndOddHeaders. this element // allow to see the odd and even footers and headers in the document. SettingAccessor.AddEvenAndOddHeadersElement(document); } return(streamDoc.GetModifiedDocument()); } }