We have demonstrated how to add text and image header footer to Word document by using Spire.Doc for Java. This article will show you how to create different headers/footers for odd and even pages on Word document in Java applications.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class oddAndEvenHeaderFooter {
    public static void main(String[] args) throws Exception {

        String input = "multiPages.docx";
        String output = "output/oddAndEvenHeaderFooter.docx";

        //load the document
        Document doc = new Document();
        doc.loadFromFile(input);

        //get the first section
        Section section = doc.getSections().get(0);

        //set the DifferentOddAndEvenPagesHeaderFooter property as true
        section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);

        //add odd header
        Paragraph P3 = section.getHeadersFooters().getOddHeader().addParagraph();
        TextRange OH = P3.appendText("Odd Header");
        P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OH.getCharacterFormat().setFontName("Arial");
        OH.getCharacterFormat().setFontSize(14);
        OH.getCharacterFormat().setTextColor(Color.BLUE);

        //add even header
        Paragraph P4 = section.getHeadersFooters().getEvenHeader().addParagraph();
        TextRange EH = P4.appendText("Even Header from E-iceblue Using Spire.Doc");
        P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EH.getCharacterFormat().setFontName("Arial");
        EH.getCharacterFormat().setFontSize(14);
        EH.getCharacterFormat().setTextColor(Color.GREEN);

        //add odd footer
        Paragraph P2 = section.getHeadersFooters().getOddFooter().addParagraph();
        TextRange OF = P2.appendText("Odd Footer");
        P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        OF.getCharacterFormat().setFontName("Arial");
        OF.getCharacterFormat().setFontSize(14);
        OF.getCharacterFormat().setTextColor(Color.BLUE);

        //add even footer
        Paragraph P1 = section.getHeadersFooters().getEvenFooter().addParagraph();
        TextRange EF = P1.appendText("Even Footer from E-iceblue Using Spire.Doc");
        EF.getCharacterFormat().setFontName("Arial");
        EF.getCharacterFormat().setFontSize(14);
        P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        EF.getCharacterFormat().setTextColor(Color.GREEN);

        //save the document
        doc.saveToFile(output, FileFormat.Docx);

    }
}

Output:

Java Add different headers/footers for odd and even pages on Word

Published in Header and Footer
Tuesday, 03 November 2020 08:31

Remove Header and Footer in Word in Java

There can be 3 types of headers and footers in a Word document, i.e. on first page, on odd pages and on even pages. This article demonstrates how to remove all the headers and footers on first page, on odd pages and on even pages in a section of a Word document using Spire.Doc for Java.

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);
    }
}
Published in Header and Footer

This article demonstrates how to adjust the header and footer distance in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;

public class AdjustHeaderFooterDistance {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Headers and Footers.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Adjust the header distance
        section.getPageSetup().setHeaderDistance(100);

        //Adjust the footer distance
        section.getPageSetup().setFooterDistance(100);

        //Save the result document
        doc.saveToFile("Output.docx", FileFormat.Docx);
    }
}

Screenshot

Header:

Adjust the Header and Footer Distance in Word in Java

Footer:

Adjust the Header and Footer Distance in Word in Java

Published in Header and Footer

This article demonstrates how to add text, images, lines and page numbers to the header or footer of a Word document by using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

public class InsertHeaderAndFooter {

    public static void main(String[] args) {

        //load a Word document
        Document document = new Document();
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //get the first section
        Section section = document.getSections().get(0);

        //call insertHeaderAndFooter method to add header and footer to the section
        insertHeaderAndFooter(section);

        //save to file
        document.saveToFile("output/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 header
        Paragraph headerParagraph = header.addParagraph();

        //insert a picture to header paragraph and set its position
        DocPicture headerPicture = headerParagraph.appendPicture("C:\\Users\\Administrator\\Desktop\\Logo.png");
        headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
        headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
        headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Bottom);

        //add text to header paragraph
        TextRange text = headerParagraph.appendText("Demo of Spire.Doc");
        text.getCharacterFormat().setFontName("Arial");
        text.getCharacterFormat().setFontSize(10);
        text.getCharacterFormat().setItalic(true);
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //set text wrapping 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 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.Right);

        //set the top border style of the footer paragraph
        footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
    }
}

Insert Header and Footer to Word in Java

Published in Header and Footer