通过使用Microsoft Word中的标头和页脚功能,您可以包括关键详细信息,例如页码,徽标,文档标题和联系信息。无论您是新的Java开发人员还是经验丰富的Java开发人员,本文都可以简单地遵循有关设计定制的标头和页脚以丰富您的文档并使受众惊叹的说明。使用Spire.Doc for Java,掌握Word标题和页脚定制的艺术从未如此简单。
- Add customized headers and footers to your Word document
- Insert a header and footer exclusively on the first page of your Word document
- Add different headers and footers to odd pages and even pages of your document
第1部分:了解spire.doc库
要将标头和页脚插入Java中的Word文档,我们将使用Spire.doc库。 java的spire.doc是一个DOC API,它使Java应用程序无需使用Microsoft Word即可读取,写入和保存Word文档。它为操纵Word文档提供了广泛的功能,包括将标头和页脚添加到Word文档中。
在我们可以使用java的spire.doc之前,我们需要将其依赖性添加到我们的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.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文档的第一页
有时,所有文档需求都是第一页上的标题和页脚。 Java的spire.doc也可以用来完成此功能。单独在文档的第一页中添加标头和页脚的准则如下:
- 创建文档类实例。
- 使用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对象的 setDifferentOddandeVenpagesHeadeerFooter 方法。
以下是将替代标题和页脚插入甚至页面和奇数页上的特定步骤:
- 创建文档类的对象。
- 使用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的标题和页脚功能来改进您的文档。
相关话题:
Convert Word to PDF
Create a Word Document
Print Word Document in Java
Add Text Watermarks or Image Watermarks to Word
Insert or Remove a Text Box in Word