using (WordprocessingDocument doc = WordprocessingDocument.Open(file, true)) { var docPart = doc.MainDocumentPart; // Count the header and footer parts and continue if there // are any. if (docPart.HeaderParts.Count() > 0 || docPart.FooterParts.Count() > 0) { docPart.DeleteParts(docPart.HeaderParts); docPart.DeleteParts(docPart.FooterParts); Document document = docPart.Document; // Remove all references to the headers and footers. // First, create a list of all descendants of type // HeaderReference. Then, navigate the list and call // Remove on each item to delete the reference. var headers = document.Descendants<HeaderReference>().ToList(); foreach (var header in headers) { header.Remove(); } // First, create a list of all descendants of type // FooterReference. Then, navigate the list and call // Remove on each item to delete the reference. var footers = document.Descendants<FooterReference>().ToList(); foreach (var footer in footers) { footer.Remove(); } // Save the changes. document.Save(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)