Header and Footer are commonly used in documents to show important information like page number, topic, chapter, etc. You can add, insert, delete or remove header and footer in Word documents (DOCX/DOC) using your Java applications. In this article, we will be learning about adding or removing headers and footers. Following are the topics that we will be exploring in detail:
- Insert or Remove Header and Footer API – Installation
- Add Header and Footer in Word Document (DOCX/DOC) using Java
- Delete Header and Footer of Word Document (DOCX/DOC) using Java
- Remove Footer from Word Document (DOCX/DOC) using Java
- Remove Header from Word Document (DOCX/DOC) using Java
Insert or Remove Header Footer API – Installation
For working with Header and Footer in Word documents, we will be using Aspose.Words for Java API where simple API calls will help us achieve the requirements. You can download the JAR file from the releases section, or copy the following Maven configurations in your project. The API will be configured via the Maven repository and you will be good to proceed with further steps explained in this article.
Repository
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>
Dependency
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.8</version>
<classifier>jdk17</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.8</version>
<classifier>javadoc</classifier>
</dependency>
</dependencies>
Add Header and Footer in Word Document (DOCX/DOC) using Java
Adding header and footer in Word documents (DOCX/DOC) is a basic yet important use case of working with word processing documents. However, different scenarios are possible. For example, you might need to add an image, a table, or only some text in the header and footer section. Moreover, sometimes the header and footer are different on the title page as compared to other pages. Sometimes the header and footer are different on even page numbers and different on odd page numbers. Therefore, we have created a concise and basic example of adding header and footer in Word documents.
Here we will insert different header footer on the title page (very first page), and different header footer on the subsequent page. Whereas, the second page will have a custom header footer including image, text, and table element. You need to follow the steps below for adding or inserting header and footer in word documents (DOCX/DOC) using Java.
- Initialize Document and DocumentBuilder class objects
- Specify that you want different header footer for the title page
- Set font properties for header text
- Create header and for subsequent pages
- Insert table and set the page number format
- Save output DOCX file
The following code is based on these steps which show how to add header and footer in Word documents (DOCX/DOC) programmatically using Java:
So far we have learned how to add or insert header and footer in word files. Below is a screenshot of how the output looks when you see it via Print Preview option.
This feature can help a lot when different DOC/DOCX documents are processed by your applications where you want to add some content as header footer of output documents.
Let us proceed to check out deleting the header and footer from MS Word files (DOCX/DOC).
Delete Header and Footer of Word Document (DOCX/DOC) using Java
You can delete the header as well as footer of word documents using Aspose.Words for Java API. As discussed above, there could be three different types of header and footer added in a document. For instance, on title page, on even pages, and odd page numbers. You can delete all headers and footers in a word file simply by following the steps below:
- Load source DOCX/DOC file
- Find header and footer on the title page, even page numbers, and odd page numbers
- Delete the required section when found
- Save output DOCX file
The code below follows these steps and shows how to delete header and footer in word documents using Java:
Remove Footer from Word Document (DOCX/DOC) using Java
We have already discussed how to remove or delete header and footer from word files. However, you may want to remove only the footers of a Word document while leaving the Header intact. These requirements are easily possible with the following steps:
- Load source word file (DOCX/DOC)
- Iterate through each Section of the word document
- Initialize HeaderFooter object
- Delete footer from the input file
- Save updated DOCX/DOC file
The following code snippet explains how to remove footer from word document using Java:
Remove Header from Word Document (DOCX/DOC) using Java
Since we have learned removing or deleting only the footer from MS Word files. Let us explore this another step further where you might need to remove only the headers from Word documents. The footers will remain the same and unaffected because our focus here is deleting the headers only. The following steps provide an outline of the procedure to be adopted for this purpose:
- Load input Word file (DOCX/DOC)
- Initialize an instance of HeaderFooter class
- Remove Headers from all section of the input document
- Save update DOCX file
Below code shows how to delete or remove the header from word documents using Java:
Conclusion
Add, Insert, Remove or Delete Header and Footer in Word documents is a very important and frequent use case of word processing. Almost all documents contain Headers and Footers, which can be manipulated with Aspose.Words for Java API as per your requirements. This article is an overview of related features where you can discuss any custom use case and requirements with us via Free Support Forums. Feel free to reach out to us for any queries and concerns.