java生成word文件带上页码,使用apache poi api创建Word文档时,如何以Y的X格式添加页码?...

这篇博客介绍了如何利用Apache POI API在Java生成的Word文档中添加页码,并在页脚显示总页数。通过在页脚的段落中插入特定的字段,如{PAGE \* MERGEFORMAT}和{NUMPAGES \* MERGEFORMAT},可以实现这一功能。示例代码展示了如何创建包含页码和总页数的Word文档。
摘要由CSDN通过智能技术生成

Is there any method specified in POI API to get the total number of pages, I am able to add page number in the footer of the document but i am not able to add the total number of pages value.

解决方案

Page count in Word is dependent of much things like font size, paragraph top/bottom margins and padding, printer settings, manually inserted page breaks and so on. So it cannot be stored directly in the file. It will be calculated on the fly while Word is rendering its pages.

But we can use fields within the footer which also calculate the page number and the total number of pages.

Example using up to apache poi 3.14:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

//import org.apache.poi.wp.usermodel.HeaderFooterType;

public class CreateWordHeaderFooter {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值