Apache POI Word - Header And Footer
Overview
This article explains how to use the XWPFHeaderFooterPolicy and XWPFHeaderFooterPolicy classes to create headers and footers for a document.
POI Word Header
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
public class HeaderFooterDoc {
public static void main(String[] args) throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
try (FileOutputStream out = new FileOutputStream("D:\\tmp\\header.docx")) {
// header policy
XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy();
// create header
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
// fill the header with content
XWPFParagraph headerParagraph = header.createParagraph();
XWPFRun headerRun = headerParagraph.createRun();
headerRun.setText("this is header");
doc.write(out);
}
}
}
}
Before creating a header, you need a header policy, which comes in three types:
- XWPFHeaderFooterPolicy.DEFAULT
- XWPFHeaderFooterPolicy.EVEN
- XWPFHeaderFooterPolicy.FIRST
After creating the header object, you can create a paragraph using header.createParagraph(). The subsequent operations should be familiar to everyone. If you are not sure how to manipulate paragraphs, you can refer to my previous article.
The content of the header is not limited to text; you can also add images to the header.
POI Word Footer
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
public class HeaderFooterDoc {
public static void main(String[] args) throws IOException {
try (XWPFDocument doc = new XWPFDocument()) {
try (FileOutputStream out = new FileOutputStream("D:\\tmp\\footer.docx")) {
XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy();
XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph footerParagraph = footer.createParagraph();
XWPFRun run = footerParagraph.createRun();
run.setText("this is footer");
doc.write(out);
}
}
}
}
The steps for manipulating footers are almost identical to those for headers, so I won't go into detail here.
Compatibility
The above code does not display headers and footers correctly in WPS.
There are two possible solutions to this problem. One option is to use alternative tools such as Aspose.Words, which is a paid tool. If cost is not an issue, you can consider using Aspose.Words. Another option is to use a template document that already has the desired header and footer settings. You can read this template document and then replace the content within the header or footer as needed. Afterwards, you can continue generating the rest of the content.
try (InputStream in = new FileInputStream("D:\\tmp\\header-template-wps.docx")) {
try (XWPFDocument doc = new XWPFDocument(in)) {
try (FileOutputStream out = new FileOutputStream("D:\\tmp\\myDoc.docx")) {
// Headers are typically present on every page, so they are a list.
List<XWPFHeader> headerList = doc.getHeaderList();
for (XWPFHeader hd : headerList) {
XWPFRun run = hd.getParagraphs().get(0).getRuns().get(0);
// Replace the text in the header
run.setText("this is override header");
}
doc.write(out);
}
}
}
In this code snippet, header-template-wps.docx is the template. You can create this template using WPS Office, add a header, and insert some text within the header. The actual content of the text doesn't matter as it will be replaced later. However, if the header content is fixed, there is no need for replacement.
Notice:Feedback requires logging into the system first.