Word 文档中的页码通常标记在文档每页的页脚中,以表示页面的顺序和总页数。页码可以方便文档创建者管理文档内容,也可以帮助文档查看者快速找到文档中的特定内容,从而提升阅读速度和阅读体验。本文将介绍如何使用 Spire.Doc for Java 通过程序在 Word 文档中添加页码。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.1.3</version>
</dependency>
</dependencies>
添加页码到 Word 文档
Word 中的页码是通过特定类型的域显示的。例如,Page 域显示当前页的页码,NumPages 域显示文档总页数,SectionPages 字段显示节的总页数。
Spire.Doc for Java 提供了 Paragraph.appendField(String fieldName, FieldType fieldType) 方法来添加各种类型的域到 Word 文档,包括 Page 域 (FieldType.Field_Page)、NumPages 域 (FieldType.Field_Num_Pages) 和 SectionPages 域 (FieldType.Field_Sect_Pages)。
以下步骤演示了如何使用 Spire.Doc for Java 在 Word 文档的页脚添加一个 Page 字段和 NumPages 字段,以显示当前页码和文档的总页数:
- 创建一个 Document 类的对象。
- 使用 Document.loadFromFile() 方法在载入 Word 文档。
- 使用 Document.getSections().get() 方法获取文档第一节。
- 使用 Section.getHeadersFooters().getFooter() 方法获取第一节的页脚。
- 在页脚添加一个段落,并使用 Paragraph.appendField(String fieldName, FieldType fieldType) 方法为该段落添加一个 Page 域和一个 NumPages 域。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
public class addPageNumbersWholeDocument {
public static void main(String[] args) {
//创建Document的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("示例.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//获取第一节的页脚
HeaderFooter footer = section.getHeadersFooters().getFooter();
//添加Page和NumPages域到页脚中并设置页码格式
Paragraph footerParagraph = footer.addParagraph();
footerParagraph.appendText("第");
footerParagraph.appendField("页码", FieldType.Field_Page);
footerParagraph.appendText("页 共");
footerParagraph.appendField("页数", FieldType.Field_Num_Pages);
footerParagraph.appendText("页");
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
footerParagraph.getStyle().getCharacterFormat().setFontSize(16);
//保存文档
document.saveToFile("文档页码.docx");
}
}
添加页码到 Word 文档并为每节重排页码
重排页码可以使页码在每节中以指定页码重新编排页码,而不从上一节的页码继续编排。
为了给 Word 文档的每一节重排页码,我们需要循环遍历文档中的所有节,并为每一节添加一个 Page 域和一个 SectionPages 域。然后使用 Section.getPageSetup().setRestartPageNumbering(true) 方法来启用重排页码,以及 Section.getPageSetup().setPageStartingNumber(int value) 方法来设置每节的起始页码。详细步骤如下:
- 创建一个 Document 的对象。
- 使用 Document.loadFromFile() 方法加载一个Word文档。
- 循环遍历文档中的各个节。
- 调用 Paragraph.appendField(String fieldName, FieldType fieldType) 方法为每个节的页脚添加 Page 域和 SectionPages 域。
- 调用 Section.getPageSetup().setRestartPageNumbering(true) 方法来启用重排页码,以及 Section.getPageSetup().setPageStartingNumber(int value) 方法来设置每个部分的起始页码。
- 使用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
public class addPageNumbersEachSection {
public static void main(String[] args) {
//创建Document的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("示例.docx");
//获取文档节数
int s = document.getSections().getCount();
//循环遍历文档中的节
for (int i = 0; i < s; i++) {
//添加Page和SectionPages域到页脚中并设置页码格式
HeaderFooter footer = document.getSections().get(i).getHeadersFooters().getFooter();
Paragraph footerParagraph = footer.addParagraph();
footerParagraph.appendText("第");
footerParagraph.appendField("页码", FieldType.Field_Page);
footerParagraph.appendText("页 共");
footerParagraph.appendField("节页数", FieldType.Field_Section_Pages);
footerParagraph.appendText("页");
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
footerParagraph.getStyle().getCharacterFormat().setFontSize(16);
//重置页码起始页并设置起始页码
if (i == s-1)
break;
else {
document.getSections().get(i + 1).getPageSetup().setRestartPageNumbering(true);
document.getSections().get(i + 1).getPageSetup().setPageStartingNumber(1);
}
}
//保存文档
document.saveToFile("每节页码.docx");
document.dispose();
}
}
在 Word 文档中的指定节中添加页码
默认情况下,插入页码到 Word 文档的某一节后,随后的章节会自动链接到前一节,并接续前节显示页码。如果只想在某一特定节添加页码,就需要将后续节与前节取消链接,然后删除后续节页脚中的内容。详细步骤如下:
- 创建一个 Document 的对象。
- 使用 Document.loadFromFile() 方法载入 Word 文档。
- 使用 Document.getSections().get() 方法获取文档的第二节。
- 调用 Section.getPageSetup().setRestartPageNumbering(true) 方法来启用重排页码,并用 Section.getPageSetup().setPageStartingNumber(int value) 方法设置该部分的起始页码。
- 调用 Paragraph.appendField(String fieldName, FieldType fieldType) 方法,向该节页脚添加 Page 域和 SectionPages 域。
- 使用 Section.getHeadersFooters().getFooter().setLinkToPrevious(false) 方法来取消后续节与第二节的链接。
- 删除后续节页脚中的内容。
- 使用 Doucment.saveToFile() 方法保存文档。
- Java
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
public class addPageNumbersSpecificSection {
public static void main(String[] args) {
//创建Document的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("示例.docx");
//获取文档第二节
Section section = document.getSections().get(1);
//获取第二节的页脚
HeaderFooter footer = section.getHeadersFooters().getFooter();
//设置从第二节开始重排页码并设置起始页码为1
section.getPageSetup().setRestartPageNumbering(true);
section.getPageSetup().setPageStartingNumber(1);
//添加Page域和SectionPages域到页脚中并设置页码格式
Paragraph footerParagraph = footer.addParagraph();
footerParagraph.appendText("第");
footerParagraph.appendField("页码", FieldType.Field_Page);
footerParagraph.appendText("页 共");
footerParagraph.appendField("页数", FieldType.Field_Section_Pages);
footerParagraph.appendText("页");
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
footerParagraph.getStyle().getCharacterFormat().setFontSize(16);
//取消将后续节链接到第二节并删除后续节页脚中的内容
document.getSections().get(2).getHeadersFooters().getFooter().setLinkToPrevious(false);
for (int i = 2; i < document.getSections().getCount(); i++) {
document.getSections().get(i).getHeadersFooters().getFooter().getChildObjects().clear();
document.getSections().get(i).getHeadersFooters().getFooter().addParagraph();
}
//保存文档
document.saveToFile("指定节页码.docx");
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。