Headers and footers are very useful for adding relative information such as page numbers, company name and logo to Word documents. In this article, we’ll introduce how to add headers and footers to a Word document in Java by using Free Spire.Doc for Java API.
In the following examples, we’ll see how to add different types of headers and footers to a Word document with free Spire.Doc for Java:
• Add simple header and footer
• Add different first page header and footer
• Add different headers and footers for odd and even pages
1. Add simple header and footer
To add header and footer to a document section, firstly, we need to use the getHeadersFooters method to get the collection of all headers and footers in the section, and then use the getHeader or getFooter method to return the simple header or footer object, next, we can add text, image or table to the header or footer.
private static void addHeaderFooter(Section section){
//Add header
HeadersFooters headersFooters = section.getHeadersFooters();
HeaderFooter header = headersFooters.getHeader();
Paragraph headerParagraph = header.addParagraph();
TextRange hText = headerParagraph.appendText("Page Header");
//Set header text format
hText.getCharacterFormat().setFontName("Calibri");
hText.getCharacterFormat().setFontSize(15f);
hText.getCharacterFormat().setTextColor(Color.blue);
//Set header paragraph format
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
//border
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Thick_Thin_Small_Gap);
headerParagraph.getFormat().getBorders().getBottom().setSpace(0.05f);
headerParagraph.getFormat().getBorders().getBottom().setColor(Color.darkGray);
//Add footer
com.spire.doc.HeaderFooter footer = section.getHeadersFooters().getFooter();
Paragraph footerParagraph = footer.addParagraph();
//Insert page number
footerParagraph.appendField("page number", FieldType.Field_Page);
footerParagraph.appendText(" of ");
footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Border
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Thick_Thin_Small_Gap);
footerParagraph.getFormat().getBorders().getTop().setSpace(0.05f);
}
2. Add different first page header and footer
We can use the setDifferentFirstPageHeaderFooter method with the PageSetup object to specify if headers/footers of the first page to be different from other pages.
private static void addDifferentFirstPageHeaderFooter(Section section){
//Specify different headers and footers for the first page
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//First page header
Paragraph headerParagraph1 = section.getHeadersFooters().getFirstPageHeader().addParagraph();
headerParagraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
//Header image
DocPicture headerImage = headerParagraph1.appendPicture("C:\\Users\\Administrator\\Desktop\\PIC\\th.jpg");
headerImage.setWidth(150f);
headerImage.setHeight(75f);
//First page footer
Paragraph footerParagraph1 = section.getHeadersFooters().getFirstPageFooter().addParagraph();
footerParagraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange fText = footerParagraph1.appendText("First Page Footer");
fText.getCharacterFormat().setFontSize(15f);
//Header for other pages
Paragraph headerParagraph2 = section.getHeadersFooters().getHeader().addParagraph();
headerParagraph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange hText = headerParagraph2.appendText("Other Page Header");
hText.getCharacterFormat().setFontSize(15f);
//Footer for other pages
Paragraph footerParagraph2 = section.getHeadersFooters().getFooter().addParagraph();
footerParagraph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
fText = footerParagraph2.appendText("Other Page Footer");
fText.getCharacterFormat().setFontSize(15f);
}
3. Add different headers and footers for odd and even pages
We can use the setDifferentOddAndEvenPagesHeaderFooter method with the PageSetup object to specify different headers/footers for odd and even pages.
private static void addOddAndEvenPagesHeaderAndFooter(Section section){
//Specify different headers and footers for odd and even pages
section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
//Odd page header
Paragraph oddHeader = section.getHeadersFooters().getOddHeader().addParagraph();
oddHeader.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange oHText = oddHeader.appendText("Odd Page Header");
//Set header text format
oHText.getCharacterFormat().setFontName("Calibri");
oHText.getCharacterFormat().setFontSize(15f);
oHText.getCharacterFormat().setTextColor(Color.blue);
//Odd page footer
Paragraph oddFooter = section.getHeadersFooters().getOddFooter().addParagraph();
oddFooter.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange oFText = oddFooter.appendText("Odd Page Footer");
oFText.getCharacterFormat().setFontName("Calibri");
oFText.getCharacterFormat().setFontSize(15f);
oFText.getCharacterFormat().setTextColor(Color.blue);
//Even page header
Paragraph evenHeader = section.getHeadersFooters().getEvenHeader().addParagraph();
evenHeader.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange eHText = evenHeader.appendText("Even Page Header");
eHText.getCharacterFormat().setFontName("Calibri");
eHText.getCharacterFormat().setFontSize(15f);
eHText.getCharacterFormat().setTextColor(Color.green);
//Even page footer
Paragraph evenFooter = section.getHeadersFooters().getEvenFooter().addParagraph();
evenFooter.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
TextRange eFText = evenFooter.appendText("Even Page Footer");
eFText.getCharacterFormat().setFontName("Calibri");
eFText.getCharacterFormat().setFontSize(15f);
eFText.getCharacterFormat().setTextColor(Color.green);
}
More information
Website: https://www.e-iceblue.com/
Support Forum: Free Spire.Doc for Java
Top comments (1)
Coo tutorial. Is this also possible with JFrame?