Java adds page numbers to word documents

Time:2020-12-16

preface

When operating word documents, you can add page numbers to make them clear, so that they can be viewed and organized later. Generally speaking, a word document contains multiple sections. We can ignore these sections, add consecutive page numbers to the whole document, and set discontinuous page numbers according to different sections. This article will demonstrate the above two cases by using java program.

Test environment construction

Before running the code, make sure that JDK and IntelliJ idea are installed on your computer. Import is also required Spire.Doc.jar Bag. There are two import methods:firstly, which can be downloaded from the official websiteFree Spire.Doc for JavaProduct package, unzip the Lib folder Spire.Doc.jar Manually import to idea.second, create one in ideaMavenprojectAnd then in the pom.xml Type the following code in the file, and finally click “import changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Code example

[example 1] Adding consecutive page numbers to a document

By default, when we add page numbers to the header or footers of the first section, other sections use the same header or footers by linking to the previous section. 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 AddContinuousNumber {
    public static void main(String[] args) {
        //Load word document
 Document document = new Document("C:UsersTest1DesktopSample.docx");
        //Gets the footers in the first section
 HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter();
        //Add paragraph to footers
 Paragraph footerParagraph = footer.addParagraph();
        //Add text, page number, and total pages fields to a paragraph
 footerParagraph.appendText (the "section");
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.appendText (page of);
        footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages);
        footerParagraph.appendText (the "page");
        //Center paragraph
 footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //Save the document
 document.saveToFile("output/ContinuousNumber.docx", FileFormat.Docx_2013);
    }
}

design sketch:

Java adds page numbers to word documents

[example 2] Adding discontinuous page numbers according to sections

Free Spire.Doc for JavaIn addition to supporting continuous page numbers for word documents, discontinuous page numbers can also be set according to the sections in the document, that is, the starting page of the next section is numbered from 1.

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 AddDiscontinuousNumber {
    public static void main(String[] args) {
        //Load word document
 Document document = new Document("C:UsersTest1DesktopSample.docx");
        //Get the first section's footers
 HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter();
        //Add paragraph to footers
 Paragraph footerParagraph = footer.addParagraph();
        //Add text, section field, page number field to paragraph
 footerParagraph.appendText (the "section");
        footerParagraph.appendField("section number", FieldType.Field_Section);
        footerParagraph.appendText (Section 1); and;
        footerParagraph.appendField("page number", FieldType.Field_Page);
        footerParagraph.appendText (the "page");
        //Center paragraph
 footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        //Determine if the document contains multiple sections
 if (document.getSections().getCount()>1) {
            //Except for the first section
 for (int i = 1; i < document.getSections().getCount(); i++) {
                //Restart pagination in the current section
 document.getSections().get(i).getPageSetup().setRestartPageNumbering(true);
                //Page from 1
 document.getSections().get(i).getPageSetup().setPageStartingNumber(1);
            }
        }
        //Save the document
 document.saveToFile("output/DiscontinuousNumbering.docx", FileFormat.Docx_2013);
    }
}

design sketch:

Java adds page numbers to word documents

(end of this article)