By utilizing the header and footer feature in Microsoft Word, you can include crucial details such as page numbers, logos, document titles, and contact information. Whether you're a new or seasoned Java developer, this article provides simple to follow instructions on designing customized headers and footers to enrich your documents and amaze your audience. With Spire.Doc for Java, mastering the art of Word header and footer customization has never been simpler.
- Add customized headers and footers to your Word document
- Insert a header and footer exclusively on the first page of your Word document
- Add different headers and footers to odd pages and even pages of your document
Part 1: Understanding the Spire.Doc Library
To insert headers and footers to word document in Java, we will make use of the Spire.Doc library. Spire.Doc for Java is a Doc API that enables Java applications to read, write and save Word documents without using Microsoft Word. It provides a wide range of features for manipulating Word documents, including add headers and footers to Word documents.
Before we can use the Spire.Doc for Java, we need to add its dependency to our Java project. We can do this by adding the following dependency to our Maven project:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>11.5.5</version>
</dependency>
</dependencies>
Part 2: Writing the Java Code
Insert simple header and footer
It's easy to add a plain header or footer to your Word document. Below are steps to add headers and footers to word document.
- Create an instance of Document class.
- Load a Word document using Document.loadFromFIle() method.
- Get the first section using Document.getSections().get() method.
- Call the custom method insertHeaderAndFooter() to insert a header and a footer into the section.
- Save the document using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Call the custom method insertHeaderAndFooter() to insert headers and footers to the section
insertHeaderAndFooter(section);
//Save the document
document.saveToFile("HeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooter(Section section) {
//Get header and footer from a section
HeaderFooter header = section.getHeadersFooters().getHeader();
HeaderFooter footer = section.getHeadersFooters().getFooter();
//Add a paragraph to the header
Paragraph headerParagraph = header.addParagraph();
//Add text to the header paragraph
TextRange text = headerParagraph.appendText("Philosophy\rWe Are Interwoven Beings");
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(12);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the bottom border style of the header paragraph
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//Add a paragraph to the footer
Paragraph footerParagraph = footer.addParagraph();
//Add Field_Page and Field_Num_Pages fields to the footer paragraph
footerParagraph.appendField("Page Number", FieldType.Field_Page);
footerParagraph.appendText(" of ");
footerParagraph.appendField("Number of Pages", FieldType.Field_Num_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the top border style of the footer paragraph
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
Insert a Header and a Footer Only into the First Page of a Word Document
Sometimes all a document needs are a header and a footer on the first page. Spire.Doc for Java can also be used to accomplish this functionality. The guidelines for adding a header and footer alone to the first page of a document are as follows:
- Create a Document class instance.
- Load a Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Make the headers and footers of the first page different from other pages using Section.getPageSetup().setDifferentFirstPageHeaderFooter() method.
- Call the custom method insertHeaderAndFooterFirst() to insert a header and a footer into the first page.
- Save the document using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Make the headers and footers of the first page different from other pages
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//Call the custom method insertHeaderAndFooterFirst() to insert a header and a footer into the first page
insertHeaderAndFooterFirst(section);
//Save the document
document.saveToFile("FirstPageHeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterFirst(Section section) {
//Get header and footer of the first page
HeaderFooter header = section.getHeadersFooters().getFirstPageHeader();
HeaderFooter footer = section.getHeadersFooters().getFirstPageFooter();
//Add a paragraph to the header
Paragraph headerParagraph = header.addParagraph();
//Add text to the header paragraph
TextRange text = headerParagraph.appendText("Philosophy");
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(14);
text.getCharacterFormat().setTextColor(Color.blue);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//Insert a picture into the header paragraph and set its position
DocPicture headerPicture = headerParagraph.appendPicture("Header.png");
headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Center);
//Set text wrapping style to Behind
headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
//Set the bottom border style of the header paragraph
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//Add a paragraph to the footer
Paragraph footerParagraph = footer.addParagraph();
//Add text to the footer paragraph
TextRange text1 = footerParagraph.appendText("We Are Interwoven Beings");
text1.getCharacterFormat().setFontName("Arial");
text1.getCharacterFormat().setFontSize(14);
text1.getCharacterFormat().setTextColor(Color.BLUE);
text1.getCharacterFormat().setItalic(true);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the top border style of the footer paragraph
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
Add different headers and footers for odd and even pages
There may be situation when we need to place distinct headers and footers on even and odd pages of a document. To provide separate headers/footers for odd and even pages, we can utilize the PageSetup object's setDifferentOddAndEvenPagesHeaderFooter method.
The following are the specific steps to insert alternative headers and footers onto even pages and odd pages:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Make the headers and footers of odd pages and even pages different using Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter() method.
- Call the custom method insertHeaderAndFooterOddEven() to insert different headers and footers into odd pages and even pages.
- Save the document using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Make the headers and footers of odd pages and even pages different
section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
//Call the custom method insertHeaderAndFooterOddEven() to insert different headers and footers into odd pages and even pages
insertHeaderAndFooterOddEven(section);
//Save the document
document.saveToFile("OddEvenHeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterOddEven(Section section) {
//Insert odd header
Paragraph P1 = section.getHeadersFooters().getOddHeader().addParagraph();
TextRange OH = P1.appendText("Odd Header");
P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OH.getCharacterFormat().setFontName("Arial");
OH.getCharacterFormat().setFontSize(16);
OH.getCharacterFormat().setTextColor(Color.RED);
//Insert even header
Paragraph P2 = section.getHeadersFooters().getEvenHeader().addParagraph();
TextRange EH = P2.appendText("Even Header");
P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EH.getCharacterFormat().setFontName("Arial");
EH.getCharacterFormat().setFontSize(16);
EH.getCharacterFormat().setTextColor(Color.RED);
//Insert odd footer
Paragraph P3 = section.getHeadersFooters().getOddFooter().addParagraph();
TextRange OF = P3.appendText("Odd Footer");
P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OF.getCharacterFormat().setFontName("Arial");
OF.getCharacterFormat().setFontSize(16);
OF.getCharacterFormat().setTextColor(Color.RED);
//Insert even footer
Paragraph P4 = section.getHeadersFooters().getEvenFooter().addParagraph();
TextRange EF = P4.appendText("Even Footer");
EF.getCharacterFormat().setFontName("Arial");
EF.getCharacterFormat().setFontSize(16);
P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EF.getCharacterFormat().setTextColor(Color.RED);
}
}
Conclusion
You may quickly put headers and footers into your Word documents and personalize them by following the instructions provided in this article. These hints and recommendations will enable you to produce polished and expert-looking documents that wow your audience, regardless of your level of experience. So use Word's header and footer functions to improve your documents.
Related topics:
Convert Word to PDF
Create a Word Document
Print Word Document in Java
Add Text Watermarks or Image Watermarks to Word
Insert or Remove a Text Box in Word
Top comments (0)