# Remove Header and Footer in Word Using Java

In the previous articles, I've introduced how to  [add text and image header/footer to Word document](https://janezzz.hashnode.dev/insert-header-and-footer-to-word-using-java-ckjmm8hix00ro6js1e7b2449e?guid=b5bd6303-536b-4530-b095-9205cff84e06&deviceId=1330dfe9-9e33-4e2a-92f7-632ea3e029ea)  as well as how to  [add different headers/footers for odd and even pages](https://janezzz.hashnode.dev/java-add-different-headersfooters-for-odd-and-even-pages-in-word-ckp56a9aq0032mds1ah03795k) . Now, this article will show you how to remove all these headers and footers in a Word document using Free Spire.Doc for Java.

**Installation (2 methods)**                
● Download the  [Free Spire.Doc for Java](https://www.e-iceblue.com/Download/doc-for-java-free.html)  and unzip it, then add the Spire.Doc.jar file to your Java application as dependency.

● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.
```xml
<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>3.9.0</version>
   </dependency>
</dependencies>
```

**Remove Header**
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HeaderFooterType;

public class RemoveHeader {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a sample Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Remove the header on the first page
        HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_First_Page);
        if (header != null)
            header.getChildObjects().clear();

        //Remove the header on the odd pages
        header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Odd);
        if (header != null)
            header.getChildObjects().clear();

        //Remove the header on the even pages
        header = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Header_Even);
        if (header != null)
            header.getChildObjects().clear();

        //Save the result document
        doc.saveToFile("RemoveHeader.docx", FileFormat.Docx_2013);
    }
}
```

**Remove Footer**
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HeaderFooterType;

public class RemoveFooter {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a sample Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Remove footer on the first page
        HeaderFooter footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_First_Page);
        if (footer != null)
            footer.getChildObjects().clear();

        //Remove footer on the odd pages
        footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Odd);
        if (footer != null)
            footer.getChildObjects().clear();

        //Remove footer on the even pages
        footer = section.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.Footer_Even);
        if (footer != null)
            footer.getChildObjects().clear();

        //Save the result document
        doc.saveToFile("RemoveFooter.docx", FileFormat.Docx_2013);
    }
}
```


