通过利用 Microsoft Word 中的页眉和页脚功能,您可以添加重要的详细信息,例如页码、徽标、文档标题和联系信息。无论您是新的还是经验丰富的 Java 开发人员,本文都提供了有关设计自定义页眉和页脚的简单易懂的说明,以丰富您的文档并让您的受众惊叹不已。借助Spire.Doc for Java,掌握 Word 页眉和页脚自定义艺术从未如此简单。
将自定义页眉和页脚添加到 Word 文档
仅在 Word 文档的第一页上插入页眉和页脚
向文档的奇数页和偶数页添加不同的页眉和页脚
第 1 部分:了解 Spire.Doc 库
要在 Java 中向 Word 文档插入页眉和页脚,我们将使用 Spire.Doc 库。Spire.Doc for Java 是一个 Doc API,它使 Java 应用程序能够在不使用 Microsoft Word 的情况下读取、写入和保存 Word 文档。它提供了用于操作 Word 文档的广泛功能,包括向 Word 文档添加页眉和页脚。
在我们使用 Spire.Doc for Java 之前,我们需要将其依赖项添加到我们的 Java 项目中。我们可以通过向 Maven 项目添加以下依赖项来实现此目的:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>11.5.5</version>
</dependency>
</dependencies>
第 2 部分:编写 Java 代码 插入简单的页眉和页脚
向 Word 文档添加普通页眉或页脚非常容易。以下是向 Word 文档添加页眉和页脚的步骤。
创建 Document 类的实例。
使用 Document.loadFromFIle() 方法加载 Word 文档。
使用 Document.getSections().get() 方法获取第一部分。
调用自定义方法 insertHeaderAndFooter() 将页眉和页脚插入到该部分中。
使用 Document.saveToFile() 方法保存文档。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class insertHeaderAndFooter {
public static void main(String[] args) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Call the custom method insertHeaderAndFooter() to insert headers and footers to the section
insertHeaderAndFooter(section);
//Save the document
document.saveToFile("HeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooter(Section section) {
//Get header and footer from a section
HeaderFooter header = section.getHeadersFooters().getHeader();
HeaderFooter footer = section.getHeadersFooters().getFooter();
//Add a paragraph to the header
Paragraph headerParagraph = header.addParagraph();
//Add text to the header paragraph
TextRange text = headerParagraph.appendText("Philosophy\rWe Are Interwoven Beings");
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(12);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the bottom border style of the header paragraph
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//Add a paragraph to the footer
Paragraph footerParagraph = footer.addParagraph();
//Add Field_Page and Field_Num_Pages fields to the footer paragraph
footerParagraph.appendField("Page Number", FieldType.Field_Page);
footerParagraph.appendText(" of ");
footerParagraph.appendField("Number of Pages", FieldType.Field_Num_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the top border style of the footer paragraph
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
仅将页眉和页脚插入到 Word 文档的首页
有时,文档所需要的只是首页上的页眉和页脚。Spire.Doc for Java 也可用于完成此功能。将页眉和页脚单独添加到文档首页的准则如下:
创建一个 Document 类实例。
使用 Document.loadFromFile() 方法加载 Word 文档。
使用 Document.getSections().get() 方法获取第一部分。
使用Section.getPageSetup().setDifferentFirstPageHeaderFooter()方法使首页的页眉和页脚与其他页面不同。
调用自定义方法 insertHeaderAndFooterFirst() 在第一页中插入页眉和页脚。
使用 Document.saveToFile() 方法保存文档。
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) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Make the headers and footers of the first page different from other pages
section.getPageSetup().setDifferentFirstPageHeaderFooter(true);
//Call the custom method insertHeaderAndFooterFirst() to insert a header and a footer into the first page
insertHeaderAndFooterFirst(section);
//Save the document
document.saveToFile("FirstPageHeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterFirst(Section section) {
//Get header and footer of the first page
HeaderFooter header = section.getHeadersFooters().getFirstPageHeader();
HeaderFooter footer = section.getHeadersFooters().getFirstPageFooter();
//Add a paragraph to the header
Paragraph headerParagraph = header.addParagraph();
//Add text to the header paragraph
TextRange text = headerParagraph.appendText("Philosophy");
text.getCharacterFormat().setFontName("Arial");
text.getCharacterFormat().setFontSize(14);
text.getCharacterFormat().setTextColor(Color.blue);
text.getCharacterFormat().setItalic(true);
headerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//Insert a picture into the header paragraph and set its position
DocPicture headerPicture = headerParagraph.appendPicture("Header.png");
headerPicture.setHorizontalAlignment(ShapeHorizontalAlignment.Left);
headerPicture.setVerticalOrigin(VerticalOrigin.Top_Margin_Area);
headerPicture.setVerticalAlignment(ShapeVerticalAlignment.Center);
//Set text wrapping style to Behind
headerPicture.setTextWrappingStyle(TextWrappingStyle.Behind);
//Set the bottom border style of the header paragraph
headerParagraph.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Single);
headerParagraph.getFormat().getBorders().getBottom().setLineWidth(1f);
//Add a paragraph to the footer
Paragraph footerParagraph = footer.addParagraph();
//Add text to the footer paragraph
TextRange text1 = footerParagraph.appendText("We Are Interwoven Beings");
text1.getCharacterFormat().setFontName("Arial");
text1.getCharacterFormat().setFontSize(14);
text1.getCharacterFormat().setTextColor(Color.BLUE);
text1.getCharacterFormat().setItalic(true);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//Set the top border style of the footer paragraph
footerParagraph.getFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
footerParagraph.getFormat().getBorders().getTop().setLineWidth(1f);
}
}
为奇数页和偶数页添加不同的页眉和页脚
有时我们需要在文档的偶数页和奇数页上放置不同的页眉和页脚。要为奇数页和偶数页提供单独的页眉/页脚,我们可以利用 PageSetup 对象的setDifferentOddAndEvenPagesHeaderFooter方法。
以下是在偶数页和奇数页上插入替代页眉和页脚的具体步骤:
创建Document类的对象。
使用 Document.loadFromFile() 方法加载 Word 文档。
使用 Document.getSections().get() 方法获取第一部分。
使用Section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter()方法使奇数页和偶数页的页眉和页脚不同。
调用自定义方法insertHeaderAndFooterOddEven(),在奇数页和偶数页中插入不同的页眉和页脚。
使用 Document.saveToFile() 方法保存文档。
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) {
//Create a Document class instance
Document document = new Document();
//Load a Word document
document.loadFromFile("We Are Interwoven Beings.docx");
//Get the first section
Section section = document.getSections().get(0);
//Make the headers and footers of odd pages and even pages different
section.getPageSetup().setDifferentOddAndEvenPagesHeaderFooter(true);
//Call the custom method insertHeaderAndFooterOddEven() to insert different headers and footers into odd pages and even pages
insertHeaderAndFooterOddEven(section);
//Save the document
document.saveToFile("OddEvenHeaderAndFooter.docx", FileFormat.Docx);
}
private static void insertHeaderAndFooterOddEven(Section section) {
//Insert odd header
Paragraph P1 = section.getHeadersFooters().getOddHeader().addParagraph();
TextRange OH = P1.appendText("Odd Header");
P1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OH.getCharacterFormat().setFontName("Arial");
OH.getCharacterFormat().setFontSize(16);
OH.getCharacterFormat().setTextColor(Color.RED);
//Insert even header
Paragraph P2 = section.getHeadersFooters().getEvenHeader().addParagraph();
TextRange EH = P2.appendText("Even Header");
P2.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EH.getCharacterFormat().setFontName("Arial");
EH.getCharacterFormat().setFontSize(16);
EH.getCharacterFormat().setTextColor(Color.RED);
//Insert odd footer
Paragraph P3 = section.getHeadersFooters().getOddFooter().addParagraph();
TextRange OF = P3.appendText("Odd Footer");
P3.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
OF.getCharacterFormat().setFontName("Arial");
OF.getCharacterFormat().setFontSize(16);
OF.getCharacterFormat().setTextColor(Color.RED);
//Insert even footer
Paragraph P4 = section.getHeadersFooters().getEvenFooter().addParagraph();
TextRange EF = P4.appendText("Even Footer");
EF.getCharacterFormat().setFontName("Arial");
EF.getCharacterFormat().setFontSize(16);
P4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
EF.getCharacterFormat().setTextColor(Color.RED);
}
}
结论
您可以按照本文提供的说明快速将页眉和页脚放入 Word 文档中并对其进行个性化设置。这些提示和建议将使您能够制作出精美且具有专业外观的文档,无论您的经验水平如何,这些文档都会让您的受众惊叹不已。因此,请使用 Word 的页眉和页脚功能来改进您的文档。