Word 文档中的页眉是位于页面顶部的文字或图片内容,而页脚则位于页面底部。页眉和页脚通常用于展示文档的重要信息,如版权、作者信息、页码,也可用于装饰文档,使文档更专业、美观。页眉、页脚可以插入到文档的每一页或第一页中,还可以在文档的奇数和偶数页插入不同的页眉、页脚。本文将介绍如何使用 Spire.Doc for Java 通过编程的方式在 Word 文档中插入页眉和页脚。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.3.0</version>
</dependency>
</dependencies>
在 Word 文档中插入页眉和页脚
要在 Word 文档中插入页眉和页脚,需要先使用 Section.getHeadersFooters().getHeader() 和 Section.getHeadersFooters().getFooter() 方法获取页眉和页脚,然后在其中添加段落,再将图片、文本、页码等添加到段落中。
插入页眉和页脚的详细操作步骤如下:
- 创建 Document 类的对象。
- 用 Document.loadFromFIle() 方法载入 Word 文档。
- 用 Document.getSections().get() 方法获取文档的第一节。
- 用自定义的 insertHeaderAndFooter() 方法插入页眉和页脚到该节中。
- 用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.Field;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//创建 Document 类的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("人类:交织的生命.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//调用自定义方法 insertHeaderAndFooter() 来插入页眉和页脚
insertHeaderAndFooter(section);
//保存文档
document.saveToFile("页眉和页脚.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooter(Section section) {
//从一个节获取页眉和页脚
HeaderFooter header = section.getHeadersFooters().getHeader();
HeaderFooter footer = section.getHeadersFooters().getFooter();
//在页眉中添加一个段落
Paragraph headerParagraph = header.addParagraph();
//添加文本到页眉段落
TextRange text = headerParagraph.appendText("哲学\r人类:交织的生命");
text.getCharacterFormat().setFontName("微软雅黑");
text.getCharacterFormat().setFontSize(10);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置页眉段落的底部线条样式
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//在页脚中添加一个段落
Paragraph footerParagraph = footer.addParagraph();
//添加 Field_Page 和 Field_Num_Pages 域到页脚段落
footerParagraph.appendField("页码", FieldType.Field_Page);
footerParagraph.appendText(" / ");
footerParagraph.appendField("页数", FieldType.Field_Num_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置页脚段落的顶部线条样式
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
只在 Word 文档的第一页中插入页眉和页脚
有时我们只需要在第一页插入页眉和页脚,这样的操作也可以通过 Spire.Doc for Java 来实现。我们可以使用 Section.getPageSetup().setDifferentFirstPageHeaderFooter() 方法来使第一页的页眉和页脚与其他页面不同,然后再插入页眉、页脚到第一页。
只插入页眉和页脚到第一页的详细操作步骤如下:
- 创建 Document 类的对象。
- 用 Document.loadFromFile() 方法载入 Word 文档。
- 用 Document.getSections().get() 方法获取文档第一节。
- 用 Document.getSections().get() 方法设置第一页的页眉和页脚与其他页不同。
- 用自定义的 insertHeaderAndFooterFirst() 方法插入页眉和页脚到第一页。
- 用 Document.saveToFile() 方法保存文档。
- Java
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 insertHeaderAndFooter {
public static void main(String[] args) {
//创建 Document 类的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("人类:交织的生命.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//设置文档第一页的页眉和页脚与其他页不同
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//调用自定义的 insertHeaderAndFooterFirst() 方法来插入页眉和页脚到第一页
insertHeaderAndFooterFirst(section);
//保存文档
document.saveToFile("第一页页眉和页脚.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterFirst(Section section) {
//获取文档第一页的页眉和页脚
HeaderFooter header = section.getHeadersFooters().getFirstPageHeader();
HeaderFooter footer = section.getHeadersFooters().getFirstPageFooter();
//添加一个段落到页眉
Paragraph headerParagraph = header.addParagraph();
//添加文本到页眉段落
TextRange text = headerParagraph.appendText("哲学");
text.getCharacterFormat().setFontName("微软雅黑");
text.getCharacterFormat().setFontSize(14);
text.getCharacterFormat().setTextColor(Color.blue);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//插入一张图片到页眉段落并设置其位置
DocPicture headerPicture = headerParagraph.appendPicture("页眉.png");
headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Top);
//设置页眉段落的底部线条样式
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//将图片的文本环绕方式设置为衬于图片下方
headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
//添加一个段落到页脚
Paragraph footerParagraph = footer.addParagraph();
//添加文本到页脚段落
TextRange text1 = footerParagraph.appendText("人类:交织的生命");
text1.getCharacterFormat().setFontName("微软雅黑");
text1.getCharacterFormat().setFontSize(14);
text1.getCharacterFormat().setTextColor(Color.blue);
text1.getCharacterFormat().setItalic(true);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//设置页脚段落的顶部线条样式
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
在 Word 文档的奇数和偶数页插入不同的页眉和页脚
我们还可能遇到需要在奇数页和偶数页中插入不同的页眉和页脚的情况。Spire.Doc for Java 提供了一个方法 Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(),可以使奇数页和偶数页的页眉、页脚不同,以满足上述需求。
在奇数页和偶数页插入不同的页眉、页脚的详细步骤如下:
- 创建 Document 类的对象。
- 用 Document.loadFromFile() 方法载入 Word 文档。
- 用 Document.getSections().get() 方法获取文档第一节。
- 用 Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter() 方法设置奇数页和偶数页的页眉、页脚不同。
- 用自定义的 insertHeaderAndFooterOddEven() 方法插入不同的页眉和页脚到奇数页和偶数页。
- 用 Document.saveToFile() 方法保存文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//创建 Document 类的对象
Document document = new Document();
//载入Word文档
document.loadFromFile("人类:交织的生命.docx");
//获取文档第一节
Section section = document.getSections().get(0);
//设置奇数页和偶数页的页眉、页脚不同
section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
//调用自定义的 insertHeaderAndFooterOddEven() 方法来插入不同的页眉和页脚到奇数页和偶数页
insertHeaderAndFooterOddEven(section);
//保存文档
document.saveToFile("奇数页偶数页页眉和页脚.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterOddEven(Section section) {
//插入页眉到奇数页
Paragraph P1 = section.getHeadersFooters().getOddHeader().addParagraph();
TextRange OH = P1.appendText("奇数页页眉");
P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OH.getCharacterFormat().setFontName("黑体");
OH.getCharacterFormat().setFontSize(16);
OH.getCharacterFormat().setTextColor(Color.BLUE);
P1.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
P1.getFormat().getBorders().getBottom().setLineWidth(1f);
//插入页眉到偶数页
Paragraph P2 = section.getHeadersFooters().getEvenHeader().addParagraph();
TextRange EH = P2.appendText("偶数页页眉");
P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EH.getCharacterFormat().setFontName("黑体");
EH.getCharacterFormat().setFontSize(16);
EH.getCharacterFormat().setTextColor(Color.BLUE);
P2.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
P2.getFormat().getBorders().getBottom().setLineWidth(1f);
//插入页脚到奇数页
Paragraph P3 = section.getHeadersFooters().getOddFooter().addParagraph();
TextRange OF = P3.appendText("奇数页页脚");
P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OF.getCharacterFormat().setFontName("黑体");
OF.getCharacterFormat().setFontSize(16);
OF.getCharacterFormat().setTextColor(Color.BLUE);
P3.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
P3.getFormat().getBorders().getTop().setLineWidth(1f);
//插入页脚到偶数页
Paragraph P4 = section.getHeadersFooters().getEvenFooter().addParagraph();
TextRange EF = P4.appendText("偶数页页脚");
EF.getCharacterFormat().setFontName("黑体");
EF.getCharacterFormat().setFontSize(16);
P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EF.getCharacterFormat().setTextColor(Color.BLUE);
P4.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
P4.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。