DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add Page Numbers to an Existing Word Documnet in Java

Page numbers make it easy for readers to remember where to start next time. Adding page numbers to a Word document is useful especially when the document is quite big. In this article, I will show you how to add continuous page numbers to a Word document containing multiple sections, and how to add different page numbers to different sections, by using Spire.Doc for Java.

Install Spire.Doc.jar

If you’re creating a non-Maven project, download the jar file from this link and add it as a dependency in your applicaiton. For Maven projects, you can easily add the jar in your applciation using the following configurations.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.doc</artifactId>
        <version>3.8.1</version>
    </dependency>
</dependencies>

Add continuous page numbers to the whole document

By default, when we add page numbers to the header or footer of the first section, other sections will link to the previous section to use the same header or footer. Therefore, we only need to set the page number in 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 ContinuousPaging {

    public static void main(String[] args) {

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

        //Get the header object of the first section 
        HeaderFooter header = document.getSections().get(0).getHeadersFooters().getHeader();

        //Add a paragraph in header 
        Paragraph headerParagraph = header.addParagraph();

        //Append text and automatic page field to the paragraph 
        headerParagraph.appendText("Page ");
        headerParagraph.appendField("currentPage", FieldType.Field_Page);
        headerParagraph.appendText(" of ");
        headerParagraph.appendField("pageNum", FieldType.Field_Num_Pages);

        //Set paragraph alignment to right
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

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

Alt Text

Add discontinuous page numbers to different sections

When we set different page numbers for different sections, we need to set a different starting page number for the next 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 DiscontinuousNumbering {

    public static void main(String[] args) {

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

        //Get the header object of the first section 
        HeaderFooter header = document.getSections().get(0).getHeadersFooters().getHeader();

        //Add a paragraph in header 
        Paragraph headerParagraph = header.addParagraph();

        //Append text and automatic page field to the paragraph 
        headerParagraph .appendText("Page ");
        headerParagraph .appendField("currentPage", FieldType.Field_Page);
        headerParagraph .appendText(" Section ");
        headerParagraph .appendField("sectionNum", FieldType.Field_Section);

        //Set paragraph alignment to right
        headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

        //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 next 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("DiscontinuousNumbering.docx", FileFormat.Docx_2013);
    }
}

Alt Text

Top comments (0)