Add Page Numbers to Word Sections in Java

This article demonstrats how to add continuous or discontinuous page numbers to different sections in a Word document by using Spire.Doc for Java.

Add continuous page numbers to different sections

By default, when we insert page numbers to the header or footer of the first section, the other sections will link to the previous section to continue using the same header or footer. So, we only need to set up page numbering for the first section.

import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class ContinuousNumbering {

    public static void main(String[] args) {

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

        //get footer object of the first section
        HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter();

        //add a paragraph to footer
        Paragraph footerParagraph = footer.addParagraph();

        //append text, automatic page field and number field to the paragraph
        footerParagraph.appendText("Page ");
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.appendText(" of ");
        footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //save to file
        document.saveToFile("output/ContinuousNumbering.docx", FileFormat.Docx_2013);
    }
}

Add discontinuous page numbers to different sections

import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;

public class DiscontinuousNumbering {

    public static void main(String[] args) {

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

        //get footer object of the first section
        HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter();

        //add a paragraph to footer
        Paragraph footerParagraph = footer.addParagraph();

        //append text and automatic page field to the paragraph
        footerParagraph.appendText("Page ");
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //determine if the document has more than one section
        if (document.getSections().getCount()>1) {

            //loop through the sections except the first one
            for (int i = 1; i < document.getSections().getCount(); i++) {

                //restart page numbering of the current section
                document.getSections().get(i).getPageSetup().setRestartPageNumbering(true);
                
                //set the starting number to 1 
                document.getSections().get(i).getPageSetup().setPageStartingNumber(1);
            }
        }

        //save to file
        document.saveToFile("output/DiscontinuousNumbering.docx", FileFormat.Docx_2013);
    }
}