Remove Header and Footer in Word in Java

Tuesday, 03 November 2020 08:31 Written by  support iceblue
Rate this item
(0 votes)

There can be 3 types of headers and footers in a Word document, i.e. on first page, on odd pages and on even pages. This article demonstrates how to remove all the headers and footers on first page, on odd pages and on even pages in a section of a Word document using Spire.Doc for Java.

Remove Header

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

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);
    }
}

Additional Info

  • tutorial_title:
Last modified on Tuesday, 31 August 2021 09:20