前言
在操作Word文档时,能够通过增加页码来使其条理清晰,以便于前期查看整顿。通常来说,一个Word文档蕴含了多个节,咱们能够漠视这些节为整个文档增加间断页码,同时也能够依据不同节来设置不间断页码。本文将通过应用Java程序来演示以上两种增加页码状况。
测试环境搭建
在运行代码前,请确保你的电脑上装置有JDK和Intellij IDEA。同时须要导入Spire.Doc.jar包。导入形式有两种:其一,在官网上下载获取Free Spire.Doc for Java产品包,解压后将lib文件夹下的Spire.Doc.jar手动导入到IDEA。其二,在IDEA中创立一个Maven我的项目,而后在pom.xml文件中键入以下代码,最初点击“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>
代码示例
【示例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 AddContinuousNumber { public static void main(String[] args) { //加载Word文档 Document document = new Document("C:UsersTest1DesktopSample.docx"); //获取第一个节中的页脚 HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter(); //增加段落到页脚 Paragraph footerParagraph = footer.addParagraph(); //增加文字、页码域和总页数域到段落 footerParagraph.appendText("第"); footerParagraph.appendField("page number", FieldType.Field_Page); footerParagraph.appendText("页 共"); footerParagraph.appendField("number of pages", FieldType.Field_Num_Pages); footerParagraph.appendText("页"); //将段落居中 footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //保存文档 document.saveToFile("output/ContinuousNumber.docx", FileFormat.Docx_2013); }}
效果图:
【示例2】依据节来增加不间断的页码
Free Spire.Doc for Java除了反对给Word文档设置间断页码外,还能够依据文档中的节来设置不间断页码,即下一节的起始页从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) { //加载Word文档 Document document = new Document("C:UsersTest1DesktopSample.docx"); //获取第一节的页脚 HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter(); //增加段落到页脚 Paragraph footerParagraph = footer.addParagraph(); //增加文本、节域、页码域到段落 footerParagraph.appendText("第"); footerParagraph.appendField("section number", FieldType.Field_Section); footerParagraph.appendText("节 第"); footerParagraph.appendField("page number", FieldType.Field_Page); footerParagraph.appendText("页"); //将段落居中 footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //判断文档是否含多个节 if (document.getSections().getCount()>1) { //遍历除第一节以外的其余节 for (int i = 1; i < document.getSections().getCount(); i++) { //在以后节从新开始编页码 document.getSections().get(i).getPageSetup().setRestartPageNumbering(true); //从1开始编页码 document.getSections().get(i).getPageSetup().setPageStartingNumber(1); } } //保存文档 document.saveToFile("output/DiscontinuousNumbering.docx", FileFormat.Docx_2013); }}
效果图:
(本文完)