There are up to three kinds of headers and footers on a word document (for first, even and odd pages) and Spire.Doc supports to insert footer and header to the word document in C#. In the following example, we will load a document with headers and footers, and we will remove all footers from all sections, but leaves headers.
Step 1: Create a new instance of word document and load the sample document from file.
Document doc = new Document(); doc.LoadFromFile("Sample.docx");
Step 2: Get the first section from file.
Section section = doc.Sections[0];
Step 3: Traverse the word document and clear all footers in different type.
foreach (Paragraph para in section.Paragraphs) { foreach (DocumentObject obj in para.ChildObjects) { HeaderFooter footer; footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage]; if (footer != null) footer.ChildObjects.Clear(); footer = section.HeadersFooters[HeaderFooterType.FooterOdd]; if (footer != null) footer.ChildObjects.Clear(); footer = section.HeadersFooters[HeaderFooterType.FooterEven]; if (footer != null) footer.ChildObjects.Clear(); }
Step 4: Save the document to file.
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
Full codes of removing footers but keeping the header on word document:
static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Sample.docx"); Section section = doc.Sections[0]; foreach (Paragraph para in section.Paragraphs) { foreach (DocumentObject obj in para.ChildObjects) { HeaderFooter footer; footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage]; if (footer != null) footer.ChildObjects.Clear(); footer = section.HeadersFooters[HeaderFooterType.FooterOdd]; if (footer != null) footer.ChildObjects.Clear(); footer = section.HeadersFooters[HeaderFooterType.FooterEven]; if (footer != null) footer.ChildObjects.Clear(); } doc.SaveToFile("Result.docx", FileFormat.Docx2013); }