In the previous articles, I've introduced how to add text and image header/footer to Word document as well as how to add different headers/footers for odd and even pages. 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 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.
<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
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);
}
}
Top comments (0)