Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

     

Add Headers and Footers to Word Document in Java

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 usingFree 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.

privatestaticvoidaddHeaderFooter(Sectionsection){//Add headerHeadersFootersheadersFooters=section.getHeadersFooters();HeaderFooterheader=headersFooters.getHeader();ParagraphheaderParagraph=header.addParagraph();TextRangehText=headerParagraph.appendText("Page Header");//Set header text formathText.getCharacterFormat().setFontName("Calibri");hText.getCharacterFormat().setFontSize(15f);hText.getCharacterFormat().setTextColor(Color.blue);//Set header paragraph formatheaderParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);//borderheaderParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Thick_Thin_Small_Gap);headerParagraph.getFormat().getBorders().getBottom().setSpace(0.05f);headerParagraph.getFormat().getBorders().getBottom().setColor(Color.darkGray);//Add footercom.spire.doc.HeaderFooterfooter=section.getHeadersFooters().getFooter();ParagraphfooterParagraph=footer.addParagraph();//Insert page numberfooterParagraph.appendField("page number",FieldType.Field_Page);footerParagraph.appendText(" of ");footerParagraph.appendField("number of pages",FieldType.Field_Num_Pages);footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);//BorderfooterParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Thick_Thin_Small_Gap);footerParagraph.getFormat().getBorders().getTop().setSpace(0.05f);}
Enter fullscreen modeExit fullscreen mode

simple header and footer

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.

privatestaticvoidaddDifferentFirstPageHeaderFooter(Sectionsection){//Specify different headers and footers for the first pagesection.getPageSetup().setDifferentFirstPageHeaderFooter(true);//First page headerParagraphheaderParagraph1=section.getHeadersFooters().getFirstPageHeader().addParagraph();headerParagraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);//Header imageDocPictureheaderImage=headerParagraph1.appendPicture("C:\\Users\\Administrator\\Desktop\\PIC\\th.jpg");headerImage.setWidth(150f);headerImage.setHeight(75f);//First page footerParagraphfooterParagraph1=section.getHeadersFooters().getFirstPageFooter().addParagraph();footerParagraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangefText=footerParagraph1.appendText("First Page Footer");fText.getCharacterFormat().setFontSize(15f);//Header for other pagesParagraphheaderParagraph2=section.getHeadersFooters().getHeader().addParagraph();headerParagraph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangehText=headerParagraph2.appendText("Other Page Header");hText.getCharacterFormat().setFontSize(15f);//Footer for other pagesParagraphfooterParagraph2=section.getHeadersFooters().getFooter().addParagraph();footerParagraph2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);fText=footerParagraph2.appendText("Other Page Footer");fText.getCharacterFormat().setFontSize(15f);}
Enter fullscreen modeExit fullscreen mode

different first page header and footer

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.

privatestaticvoidaddOddAndEvenPagesHeaderAndFooter(Sectionsection){//Specify different headers and footers for odd and even pagessection.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);//Odd page headerParagraphoddHeader=section.getHeadersFooters().getOddHeader().addParagraph();oddHeader.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangeoHText=oddHeader.appendText("Odd Page Header");//Set header text formatoHText.getCharacterFormat().setFontName("Calibri");oHText.getCharacterFormat().setFontSize(15f);oHText.getCharacterFormat().setTextColor(Color.blue);//Odd page footerParagraphoddFooter=section.getHeadersFooters().getOddFooter().addParagraph();oddFooter.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangeoFText=oddFooter.appendText("Odd Page Footer");oFText.getCharacterFormat().setFontName("Calibri");oFText.getCharacterFormat().setFontSize(15f);oFText.getCharacterFormat().setTextColor(Color.blue);//Even page headerParagraphevenHeader=section.getHeadersFooters().getEvenHeader().addParagraph();evenHeader.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangeeHText=evenHeader.appendText("Even Page Header");eHText.getCharacterFormat().setFontName("Calibri");eHText.getCharacterFormat().setFontSize(15f);eHText.getCharacterFormat().setTextColor(Color.green);//Even page footerParagraphevenFooter=section.getHeadersFooters().getEvenFooter().addParagraph();evenFooter.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);TextRangeeFText=evenFooter.appendText("Even Page Footer");eFText.getCharacterFormat().setFontName("Calibri");eFText.getCharacterFormat().setFontSize(15f);eFText.getCharacterFormat().setTextColor(Color.green);}
Enter fullscreen modeExit fullscreen mode

different headers and footers for odd and even pages

More information

Website:https://www.e-iceblue.com/

Support Forum:Free Spire.Doc for Java

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jwanoo profile image
jwanOo
Binary Enthusiast
  • Location
    Berlin
  • Work
    System Administrator Apprentice
  • Joined

Coo tutorial. Is this also possible with JFrame?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp