Java 在Word中设置页码——不连续设置页码

2025-10-22 18:06:51

1、导入方法1:通过E-iceblue官网下载Free Spire.Doc for Java 的文件包并解压,在IDEA程序中导入Spire.Doc.jar文件(jar文件在文件夹lib下获取)

2、导入方法2:通过Maven仓库导入。

1、import com.spire.doc.*;


import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.PageNumberStyle;
import com.spire.doc.documents.Paragraph;


public class DifferentPageNumber {
   public static void main(String[]args){
       //加载测试文档
       Document doc = new  Document("test.docx");


       //添加页码到第一节
       HeaderFooter footer= doc.getSections().get(0).getHeadersFooters().getFooter();
       Paragraph footerpara = footer.addParagraph();
       footerpara.appendField("Page Number",FieldType.Field_Page);
       footerpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
       //设置第一节页码数字格式为罗马数字
       doc.getSections().get(0).getPageSetup().setPageNumberStyle(PageNumberStyle.Roman_Lower);


       //设置第二节页码数字格式为阿拉伯数字
       doc.getSections().get(1).getPageSetup().setPageNumberStyle(PageNumberStyle.Arabic);
       //设置第二节页码从新开始编码,并设置起始页码数
       doc.getSections().get(1).getPageSetup().setRestartPageNumbering(true);
       doc.getSections().get(1).getPageSetup().setPageStartingNumber(1);


       //保存文档
       doc.saveToFile("restartnumbering.docx",FileFormat.Docx_2010);
   }
}

2、页码设置效果:

Java 在Word中设置页码——不连续设置页码

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢