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

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 {

public static void main(String[] args) throws Exception {

XWPFDocument doc= new XWPFDocument();

// the body content

XWPFParagraph paragraph = doc.createParagraph();

XWPFRun run=paragraph.createRun();

run.setText("The Body:");

paragraph = doc.createParagraph();

run=paragraph.createRun();

run.setText("Lorem ipsum.... page 1");

paragraph = doc.createParagraph();

run=paragraph.createRun();

run.addBreak(BreakType.PAGE);

run.setText("Lorem ipsum.... page 2");

paragraph = doc.createParagraph();

run=paragraph.createRun();

run.addBreak(BreakType.PAGE);

run.setText("Lorem ipsum.... page 3");

// create header-footer

XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();

if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

// create header start

XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

//XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);

paragraph = header.getParagraphArray(0);

if (paragraph == null) paragraph = header.createParagraph();

paragraph.setAlignment(ParagraphAlignment.LEFT);

run = paragraph.createRun();

run.setText("The Header:");

// create footer start

XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

//XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);

paragraph = footer.getParagraphArray(0);

if (paragraph == null) paragraph = footer.createParagraph();

paragraph.setAlignment(ParagraphAlignment.CENTER);

run = paragraph.createRun();

run.setText("Page ");

paragraph.getCTP().addNewFldSimple().setInstr("PAGE \\* MERGEFORMAT");

run = paragraph.createRun();

run.setText(" of ");

paragraph.getCTP().addNewFldSimple().setInstr("NUMPAGES \\* MERGEFORMAT");

FileOutputStream out = new FileOutputStream("CreateWordHeaderFooter.docx");

doc.write(out);

out.close();

doc.close();

}

}

The fields in Word then are {PAGE \* MERGEFORMAT} and {NUMPAGES \* MERGEFORMAT}.

For using current apache poi 4.1.2 one can do this without XWPFHeaderFooterPolicy using XWPFDocument.createHeader, XWPFDocument.createFooter and HeaderFooterType:

...

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

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

...

// create header-footer

//XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();

//if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

// create header start

//XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

XWPFHeader header = doc.createHeader(HeaderFooterType.DEFAULT);

...

// create footer start

//XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

XWPFFooter footer = doc.createFooter(HeaderFooterType.DEFAULT);

...

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/550411.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java给你的初步印象_Java之初印象

Java语言的跨平台性:Java语言的编程过程:只要实现了特定平台下的解释器程序(JVM),Java字节码就能通过解释器程序在该平台下运行,这是java跨平台的根本,当前并不是在所有平台下都有相应的Java解释器程序,这也是Java并不是在所有平台下都能运行的原因,它只能在实现了Java解释器程…

java 字符串实例_Java字符串实例

需求1:自己实现trim的方法。需求2: 获取上传文件名 "D:\\20120512\\day12\\Demo1.java"。需求3:将字符串对象中存储的字符反序。 新中国好 -----> 好国中新需求4: 求一个子串在整串中出现的次数 。public cl…

java过滤器场景_java servlet过滤器应用场景

1、统一POST请求中文字符编码的过滤器如何 实现:将每个Servlet共有的代码提取出来。2、禁止浏览器缓存所有动态页面的过滤器如何 实现:response.setDateHeader("Expires",-1);response.setHeader("Cache-Control","no-cache&qu…

java appender_log4j的Appenders配置方法

因为是刚开始使用log4j,很多配置方面的东西都不懂,记录下。下面是我用sts(spring tool suite)新建spring mvc项目的时候,帮我自动生成的一个log4j.xml配置文件。我要说的就是appenders中的配置内容输出方式:org.apache.log4j.cons…

php js跨域上传文件,Jquery实现跨域异步上传文件步骤详解

这次给大家带来Jquery实现跨域异步上传文件步骤详解,Jquery实现跨域异步上传文件的注意事项有哪些,下面就是实战案例,一起来看一下。先说明白这个跨域异步上传功能我们借助了Jquery.form插件,它在异步表单方面很有成效&#xff0c…

查看php计划任务,php如何实现定时执行计划任务?

我们在之前的文章中给大家介绍了php计划任务的实现原理,相信对此小伙伴们都理解了php计划任务的原理,那么我们今天继续给大家介绍一下php如何实现定时执行计划任务!一、windows计划任务1、写一个PHP程序,命名为test.php&#xff0…

微信小程序实现登录功能php后台,微信小程序实现登录功能的逻辑整理

为了让大家更好的开发微信小程序,本文主要和大家分享微信小程序登录逻辑整理,希望能帮助到大家。注册/登录小程序端" style"margin: 0.8em 0px; padding: 0px; box-sizing: border-box; font-weight: 100; line-height: 1.3em; font-size: 2.1…

php js offset,获取元素的偏移量offset实例详解

问题:如果获取元素距离文档顶部的距离?[javascript] view plain copyvar rect$(#elem)[0].getBoundingClientRect();//获取元素距离文档顶部的距离var toprect.top(window.pageYOffset||document.documentElement.scrollTop)-(document.documentElement.clientTop|…

python文件下载速度 装饰器_python学习笔记之---装饰器

# -*- coding:utf-8 -*-示例1: 最简单的函数,表示调用了两次def myfunc():print ("myfunc() called.")myfunc()myfunc()E:\>py -3 a.pymyfunc() called.myfunc() called.第二步:使用装饰函数在函数执行前和执行后分别附加额外功能示例2: 替换函数(装饰…

php 日期 间隔,PHP实现计算日期间隔天数的方法

这篇文章主要介绍了PHP编程计算日期间隔天数的方法,涉及php日期与时间的转换与运算相关操作技巧,需要的朋友可以参考下刚开始在没有查PHP手册的情况下,用比较老套方法也折腾出来了,代码是这样子实现的:$date_1 date(Y-m-d);$date_2 2012-07-…

Vue02 -- 生命周期

<!DOCTYPE html> <html> <head><title>Vue --- 生命周期</title></head><body><div id"app"><input type"text" name"" v-model"a" placeholder"你的名字"><h1&g…

php 删除上传文件,php实现文件上传、下载和删除的方法

这篇文章主要为大家详细介绍了php文件上传、下载和删除示例,具有一定的参考价值&#xff0c;感兴趣的小伙伴们可以参考一下php文件上传、下载和删除示例大体思路如下&#xff0c;具体内容如下一.文件上传1.把上传文件的区域做出来p12.把显示文件的区域做出来p23.提交表单&#…

软件工程详细设计说明书_软件工程导论知识点梳理之简答题

1. 软件危机的表现形式对软件开发成本和进度估计不准确已完成的软件不符合用户需求软件产品质量差&#xff0c;可靠性得不到保证软件产品可维护性差软件成本在计算机总成本中的比例逐渐变大软件开发生产率提高速度比不上计算机应用速度2. 产生软件危机的原因(1)软件是计算机系统…

css中absolute设置问题和如何让div居中

今天设置多个div到页面正中间的时候&#xff0c;在第一层<div class"map">中设置如下&#xff1a; .map{ position&#xff1a;absolute&#xff1b; top:50%; left:50% transform: translate(-50%, -50%); } 该div就移到页面的正中间&#xff0c;达到预定效果…

php模板意思,php中的 是什么意思

php调用类的内部静态成员&#xff0c;或者是类之间调用就要用两个冒号(::)。说明&#xff1a;“::”符号可以认为是与C语言中的“.”相似的&#xff0c;而它更像C中(Perl)的::类范围操作符。示例&#xff1a;{$0;(){//}(){();$;}};/*C语言中的*/a::b::c();//C中的函数$a::b::c;…

程序员为什么老得快_这段 Python 代码让程序员赚 300W,公司已确认!网友:神操作!...

点击上方“Python大本营”&#xff0c;选择“置顶公众号”python大本营 IT人的职业提升平台Python到底还能给人多少惊喜&#xff1f;笔者最近看到了这两天关于Python最热门的话题&#xff0c;关于《地产大佬潘石屹学Python的原因》&#xff0c;结果被这个回答惊到了&#xff1…

Mercedes-Benz won’t start| Step by Step Troubleshooting Guide

Mercedes won’t start or turn over? Are you experiencing Mercedes-Benz no start problems? Key won’t turn at all? Engine turning over but the car will not start? Maybe it finally starts, runs for a few seconds and then dies. These are common Mercedes-…

php如何设置页面布局,excel页面布局怎么调整

excel页面布局调整的方法&#xff1a;首先点击菜单的页面布局&#xff0c;选择纸张大小&#xff1b;然后点击“纸张方向”&#xff0c;单击以选择横向或者纵向&#xff1b;最后点击“页边距”即可。点击菜单——页面布局&#xff0c;工具栏将出现页面布局的许多项目&#xff0c…

无法获取未定义或 null 引用的属性“value”_SpringBoot之Spring@Value属性注入使用详解

在使用Spring框架的项目中&#xff0c;Value是使用比较频繁的注解之一&#xff0c;它的作用是将配置文件中key对应的值赋值给它标注的属性。在日常使用中我们常用的功能都比较简单&#xff0c;本篇文章系统的带大家来了解一下Value的使用方法。Value注入支持形式Value属性注入功…

0x11 栈

【例题】Push,Pop,GetMin 手写一个栈 1 #include <iostream>2 #include <cstdio>3 #include <cmath>4 #include <cstring>5 #include <algorithm>6 #include <queue>7 using namespace std;8 const int maxn1000000;9 int stack[maxn], m[…