Java 添加Word页眉、页脚
本篇文章将介绍通过java程序来添加Word页眉页脚的方法。鉴于在不同文档中,对页眉页脚的操作要求不同,文章将分别从以下几种情况来阐述:
1.添加页眉页脚
- 添加图片到页眉
- 添加文本到页眉
- 添加页码
2.设置奇偶页不同的页眉页脚
3.设置首页页眉页脚不同
4.不连续设置页码(即对不同章节的内容设置不同页码)
5.锁定页眉页脚(不可编辑页眉页脚)
6.删除页眉页脚
- 删除全部页眉页脚
- 删除首页页眉页脚
使用工具:Free Spire.Doc for Java
方法一:直接获取文件包里的jar文件,导入到程序(需要先下载文件包)。
方法二:通过Maven导入到程序。
【示例1】添加页眉页脚(文本、图片、页码)
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*; public class AddHeaderFooter {
public static void main(String[]args){
//加载需要添加页眉页脚的文档
Document doc= new Document("test.docx");
Section sec = doc.getSections().get(0); //调用方法添加页眉页脚
AddHeaderFooter(sec); //保存文档
doc.saveToFile("AddHeaderFooter.docx");
}
//自定义方法来添加图片、文字页眉及页码
private static void AddHeaderFooter(Section sec){
//加载图片添加到页眉,并设置图片在段落中的对齐方式
HeaderFooter header = sec.getHeadersFooters().getHeader();
Paragraph hpara= header.addParagraph();
DocPicture pic =hpara.appendPicture("1.png");
pic.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
pic.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
pic.setVerticalAlignment(ShapeVerticalAlignment.Center);
//添加文字到页眉,并设置字体、字号、字体加粗、对齐方式
TextRange txt = hpara.appendText("青年时报");
txt.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);
txt.getCharacterFormat().setTextColor(Color.GRAY);
txt.getCharacterFormat().setFontName("仿宋");
txt.getCharacterFormat().setFontSize(12f);
txt.getCharacterFormat().setBold(true);
hpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//设置图片的文本环绕方式、页眉底部边线(粗细、间距)
pic.setTextWrappingStyle(TextWrappingStyle.Behind);
hpara.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
hpara.getFormat().getBorders().getBottom().setLineWidth(0.5f);
hpara.getFormat().getBorders().setSpace(2f); //添加页码到页脚,并设置页脚对齐方式,顶部边线粗细、间距
HeaderFooter footer = sec.getHeadersFooters().getFooter();
Paragraph fpara= footer.addParagraph();
fpara.appendField("页码",FieldType.Field_Page);
fpara.appendText("/");
fpara.appendField("总页数",FieldType.Field_Num_Pages);
fpara.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
fpara.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
fpara.getFormat().getBorders().getTop().setLineWidth(1f);
fpara.getFormat().getBorders().getTop().setSpace(2f);
}
}
页眉页脚添加效果:

【示例2】设置奇偶页页眉页脚不同
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange; import java.awt.*; public class HeaderFooterForOddAndEvenPages {
public static void main(String[] args){
//加载测试文档
Document doc = new Document("test.docx");
Section sec = doc.getSections().get(0); //设置奇偶页页眉页脚不同
sec.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true); //设置奇数页页眉页脚
HeaderFooter oddheader = sec.getHeadersFooters().getOddHeader();
Paragraph para1 = oddheader.addParagraph();
TextRange textRange1 = para1.appendText("奇数页页眉");
para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
textRange1.getCharacterFormat().setTextColor(Color.ORANGE);
textRange1.getCharacterFormat().setBold(true);
HeaderFooter oddfooter = sec.getHeadersFooters().getFooter();
Paragraph para2 = oddfooter.addParagraph();
TextRange textRange2 = para2.appendText("奇数页页脚");
para2.getFormat().setHorizontalAlignment(HorizontalAlignment.Left);
textRange2.getCharacterFormat().setTextColor(Color.ORANGE);
textRange2.getCharacterFormat().setBold(true); //设置偶数页页眉页脚
HeaderFooter evenheader = sec.getHeadersFooters().getEvenHeader();
Paragraph para3 = evenheader.addParagraph();
TextRange textRange3 = para3.appendText("偶数页页眉");
para3.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
textRange3.getCharacterFormat().setTextColor(Color.BLUE);
textRange3.getCharacterFormat().setBold(true);
HeaderFooter evenfooter = sec.getHeadersFooters().getEvenFooter();
Paragraph para4 = evenfooter.addParagraph();
TextRange textRange4 = para4.appendText("偶数页页脚");
para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
textRange4.getCharacterFormat().setTextColor(Color.BLUE);
textRange4.getCharacterFormat().setBold(true); //保存文档
doc.saveToFile("result.docx",FileFormat.Docx_2010);
} }
奇偶数页眉页脚设置效果:


【示例3】设置首页页眉页脚不同
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange; import java.awt.*; public class HeaderFooterDifferentFromFirstPage {
public static void main(String[] args){
//加载测试文的
Document doc = new Document("test.docx");
Section sec = doc.getSections().get(0); //设置首页页眉页脚不同
sec.getPageSetup().setDifferentFirstPageHeaderFooter(true); //添加首页页眉页脚
HeaderFooter firstpageheader = sec.getHeadersFooters().getFirstPageHeader();
Paragraph para1 = firstpageheader.addParagraph();
TextRange textRange1 = para1.appendText("首页页眉");
para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
textRange1.getCharacterFormat().setBold(true);
textRange1.getCharacterFormat().setTextColor(Color.GREEN);
HeaderFooter firstpagefooter = sec.getHeadersFooters().getFirstPageFooter();
Paragraph para2 = firstpagefooter.addParagraph();
TextRange textRange2 = para2.appendText("首页页脚");
para2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
textRange2.getCharacterFormat().setBold(true);
textRange2.getCharacterFormat().setTextColor(Color.GREEN); //添加页眉页脚到其他页面
Paragraph para3 = sec.getHeadersFooters().getHeader().addParagraph();
para3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange textRange3 = para3.appendText("非首页页眉");
textRange3.getCharacterFormat().setBold(true);
Paragraph para4 = sec.getHeadersFooters().getFooter().addParagraph();
para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange textRange4 = para4.appendText("非首页页脚");
textRange4.getCharacterFormat().setBold(true); //保存文档
doc.saveToFile("result2.docx",FileFormat.Docx_2010);
}
}
页眉页脚设置效果:


【示例4】不连续设置页码
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);
}
}
页码设置效果:

【示例5】锁定页眉页脚
import com.spire.doc.*;
public class LockHeaderFooter {
public static void main(String[]args){
//加载测试文档
Document doc = new Document("sample.docx");
//获取第一节
Section sec = doc.getSections().get(0);
//设置保护类型及密码
doc.protect(ProtectionType.Allow_Only_Form_Fields,"123");
sec.setProtectForm(false);
//保存文档
doc.saveToFile("LockHeaderFooter.docx",FileFormat.Docx_2010);
}
}
锁定页眉页脚后,文档中除页眉页脚外其他内容可正常编辑。
【示例6】删除页眉页脚
删除所有页眉页脚
import com.spire.doc.*; public class DeleteAllHeaderFooter {
public static void main(String[]args){
//加载测试文档
Document doc = new Document("sample.docx");
//获取第一节
Section sec = doc.getSections().get(0); //删除页眉
sec.getHeadersFooters().getHeader().getChildObjects().clear(); //删除页脚
sec.getHeadersFooters().getFooter().getChildObjects().clear(); //保存文档
doc.saveToFile("DeleteAllHeaderFooter.docx");
}
}
删除效果前后对比:


删除首页页眉页脚
import com.spire.doc.*;
public class DeleteHeaderFooterOfFirstPage {
public static void main(String[]args){
//加载测试文档
Document doc = new Document("sample.docx");
//获取第一节
Section sec = doc.getSections().get(0);
//设置首页页眉页脚不同
sec.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//删除首页页眉页脚
sec.getHeadersFooters().getFirstPageHeader().getChildObjects().clear();
sec.getHeadersFooters().getFirstPageFooter().getChildObjects().clear();
//保存文档
doc.saveToFile("DeleteHeaderFooterOfFirstPage.docx",FileFormat.Docx_2010);
}
}
首页页眉页脚删除效果:

(本文完)
转载请注明出处!
Java 添加Word页眉、页脚的更多相关文章
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...
- C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚
前言 本文是对Word页眉页脚的操作方法的进一步的阐述.在“C# 添加Word页眉页脚.页码”一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但是对于比较复杂一点的 ...
- ★itext-为pdf文件添加页眉页脚 | 3步完成 |
由于上一篇自定义生成pdf的功能需求又增加了,需要加上页码.所以本博客诞生了~ 1. 通过继承PdfPageEventHelper类,实现需要实现的方法 import com.lowagie.text ...
- itext 生成pdf文件添加页眉页脚
原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...
- iText + Freemarker实现pdf的导出,支持中文、css以及图片,页眉页脚,页眉添加图片
本文为了记录自己的实现过程,具体的实现步骤是参考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 来实现的,只是在他的基础上添加了页眉页脚及页眉 ...
- 使用C#在word中插入页眉页脚
//插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...
- word页眉页脚 首页 索引 正文各不同的处理方法
1.在目录和正文之间,加入分隔符——分节符——下一页,然后再添加页眉页脚,然后再添加索引:
- C# 如何添加Excel页眉页脚(图片、文字、奇偶页不同)
简介 我们可以通过代码编程来对Excel工作表实现很多操作,在下面的示例中,将介绍如何来添加Excel页眉.页脚.在页眉处,我们可以添加文字,如公司名称.页码.工作表名.日期等,也可以添加图片,如LO ...
- 用什么方法给PDF添加页眉页脚
我们所看到的书本中都会设置好有页眉页脚,那么电子书想要添加页眉页脚要怎么操作呢,用什么方法可以在PDF中添加页眉页脚呢,今天就为大家分享一下,如何在电子文件中添加页眉页脚,想知道的小伙伴们就一起来看看 ...
随机推荐
- Vue大纲
Vue框架 Vue ---- vue的基本使用 文本/事件/属性指令 补充: js面向对象 js函数 Vue ---- 表单指令 条件指令 循环指令 分隔符 过滤器 计算属性 监听属性 Vue --- ...
- C#中如何用最少的(20元,10元,5元,1元)付款
- 浅析堆栈段,BBS段,数据段,代码段
文章目录 1. 进程,线程 2. 堆栈段 3. BBS段 4. 代码段 5. 数据段 6. 例子 7. 总结 1. 进程,线程 所谓进程是指在系统中能独立运行并作为资源分配的基本单位,程序段,数据段和 ...
- rabbitmq~消息失败后重试达到 TTL放到死信队列(事务型消息补偿机制)
这是一个基于消息的分布式事务的一部分,主要通过消息来实现,生产者把消息发到队列后,由消费方去执行剩下的逻辑,而当消费方处理失败后,我们需要进行重试,即为了最现数据的最终一致性,在rabbitmq里,它 ...
- WPF 3D足球导览
根据博文:https://www.cnblogs.com/duel/p/regular3dpoints.html获取足球的3D坐标后,在每一个坐标位置创建一个ModelVisual3D元素,既能实现炫 ...
- 集合系列 Map(十四):WeakedHashMap
WeakedHashMap 也是 Map 集合的哈希实现,但其余 HashMap 的不同之处在于.其每个节点的 value 引用是弱引用,可以方便 GC 回收. public class WeakHa ...
- Mysql 的异常:The last packet successfully received from the server was 90 milliseconds ago. The last packet sent successfully to the server was 43,603,303 milliseconds ago. is longer than the server con
调试一个程序, 调试到一半, 下班回家, 程序卡在了某一行, 第二天早上回来一看, 发现了异常: Wed Sep :: GMT+: WARN: Establishing SSL connection ...
- 解决Entity 实体类中加了@Id 注解后仍然出现org.hibernate.AnnotationException: No identifier specified for entity 错误
启动报错如下图所示: 解决方案: 查看网上的资料,大部分都说在实体类中没有添加加主键的注解@Id,这个是必须的.但是我的实体类中明明已经添加了@Id,为什么还会报这个错误呢? 后来检查了很久,发现是我 ...
- 【译】浅谈SOLID原则
SOLID原则是一种编码的标准,为了避免不良设计,所有的软件开发人员都应该清楚这些原则.SOLID原则是由Robert C Martin推广并被广泛引用于面向对象编程中.正确使用这些规范将提升你的代码 ...
- Make a Property Calculable 使属性可计算
In this lesson, you will learn how to manage calculated properties. For this purpose, the Payment cl ...