Home  >  Apache API

Apache POI-XWPF: Write MS Word DOCX Header, Footer and Body Paragraph Example

By Arvind Rai, February 04, 2015
In this page we will learn how to write content in MS word DOCX header, footer and body paragraph. To write header and footer, Apache POI provides methods as XWPFHeaderFooterPolicy.createHeader and XWPFHeaderFooterPolicy.createFooter respectively. To write Body paragraph, we need to use XWPFParagraph and XWPFRun API.

Write Header and Footer Paragraph

To write header and footer, we need to create XWPFParagraph instance passing the instance of CTP and XWPFDocument . Now call XWPFHeaderFooterPolicy.createHeader for header and XWPFHeaderFooterPolicy.createFooter for footer.

Write Body Paragraph using XWPFRun

To write body content, create the instance of XWPFParagraph and then create the instance of XWPFRun. Configure other settings like format or bold and italic text etc to XWPFRun instance.

Example for Header, Footer and Body Paragraph

Find the complete example to write header, footer and body paragraph.
WriteDOCX.java
package com.concretepage;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
public class WriteDOCX {
	public static void main(String[] args) {
	   try {
		XWPFDocument docx = new XWPFDocument();
	        CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
		XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
			
		//write header content
		CTP ctpHeader = CTP.Factory.newInstance();
	        CTR ctrHeader = ctpHeader.addNewR();
		CTText ctHeader = ctrHeader.addNewT();
		String headerText = "This is header";
		ctHeader.setStringValue(headerText);	
		XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx);
	        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
	        parsHeader[0] = headerParagraph;
	        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
	        
		//write footer content
		CTP ctpFooter = CTP.Factory.newInstance();
		CTR ctrFooter = ctpFooter.addNewR();
		CTText ctFooter = ctrFooter.addNewT();
		String footerText = "This is footer";
		ctFooter.setStringValue(footerText);	
		XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, docx);
	        XWPFParagraph[] parsFooter = new XWPFParagraph[1];
	        parsFooter[0] = footerParagraph;
		policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
			
		//write body content
		XWPFParagraph bodyParagraph = docx.createParagraph();
		bodyParagraph.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun r = bodyParagraph.createRun();
		r.setBold(true);
	        r.setText("This is body content.");
			
	        FileOutputStream out = new FileOutputStream("D:/docx/write-test.docx");
	        docx.write(out);
	        out.close();
	        System.out.println("Done");
	    } catch (Exception ex) {
		   ex.printStackTrace();
	    }
	}
} 

Gradle File for Apache POI-XWPF

Find the gradle file to resolve JAR dependency of Apache POI-XWPF.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'ApachePOI'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.apache.poi:poi-ooxml:3.11'
} 

Output

After running the example, a DOCX file will be created as below.
Apache POI-XWPF:  Write MS Word DOCX Header, Footer and Body Paragraph Example
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us