
- Apache POI Word - Home
- Apache POI Word - Overview
- Apache POI Word - Installation
- Apache POI Word - Core Classes
- Apache POI Word - Document
- Apache POI Word - Paragraph
- Apache POI Word - Borders
- Apache POI Word - Tables
- Apache POI Word - Font & Alignment
- Apache POI Word - Text Extraction
- Apache POI Word Resources
- Apache POI Word - Quick Guide
- Apache POI Word - Useful Resources
- Apache POI Word - Discussion
Apache POI Word - Quick Guide
Apache POI Word - Overview
Many a time, a software application is required to generate reference documents in Microsoft Word file format. Sometimes, an application is even expected to receive Word files as input data.
Any Java programmer who wants to produce MS-Office files as output must use a predefined and read-only API to do so.
What is Apache POI?
Apache POI is a popular API that allows programmers to create, modify, and display MS-Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify MS-Office files using Java program. It contains classes and methods to decode the user input data or a file into MS-Office documents.
Components of Apache POI
Apache POI contains classes and methods to work on all OLE2 Compound documents of MS-Office. The list of components of this API is given below −
POIFS (Poor Obfuscation Implementation File System) − This component is the basic factor of all other POI elements. It is used to read different files explicitly.
HSSF (Horrible SpreadSheet Format) − It is used to read and write .xls format of MS-Excel files.
XSSF (XML SpreadSheet Format) − It is used for .xlsx file format of MS-Excel.
HPSF (Horrible Property Set Format) − It is used to extract property sets of the MS-Office files.
HWPF (Horrible Word Processor Format) − It is used to read and write .doc extension files of MS-Word.
XWPF (XML Word Processor Format) − It is used to read and write .docx extension files of MS-Word.
HSLF (Horrible Slide Layout Format) − It is used to read, create, and edit PowerPoint presentations.
HDGF (Horrible DiaGram Format) − It contains classes and methods for MS-Visio binary files.
HPBF (Horrible PuBlisher Format) − It is used to read and write MS-Publisher files.
This tutorial guides you through the process of working on MS-Word files using Java. Therefore the discussion is confined to HWPF and XWPF components.
Note − OLDER VERSIONS OF POI SUPPORT BINARY FILE FORMATS SUCH AS DOC, XLS, PPT, ETC. VERSION 3.5 ONWARDS, POI SUPPORTS OOXML FILE FORMATS OF MS-OFFICE SUCH AS DOCX, XLSX, PPTX, ETC.
Apache POI Word - Installation
This chapter will guide you on how to prepare a development environment to start your work with Apache POI Word. It will also teach you how to set up JDK on your machine before you set up Apache POI −
Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
If you are running Windows and have installed the JDK in C:\jdk-19, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk-19;%PATH% set JAVA_HOME=C:\jdk-19
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-19 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-19/bin:$PATH setenv JAVA_HOME /usr/local/jdk-19
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Popular Java Editors
To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following −
Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
Netbeans − It is a Java IDE that is open-source and free, which can be downloaded from www.netbeans.org/index.html.
Eclipse − It is also a Java IDE developed by the eclipse open-source community and can be downloaded from www.eclipse.org.
Step 3 − Install Apache POI Library
Download the latest version of Apache POI from https://poi.apache.org/download.html and unzip its contents to a folder from where the required libraries can be linked to your Java program. Let us assume the files are collected in a folder on C drive.
Add the complete path of the required jars as shown below to the CLASSPATH.
Sr.No. | Platform & Description |
---|---|
1 |
Windows Append the following strings to the end of the user variable CLASSPATH − C:\poi-bin-5.4.1\poi-5.4.1.jar; C:\poi-bin-5.4.1\lib\commons-codec-1.18.0.jar; C:\poi-bin-5.4.1\lib\commons-collections4-4.4.jar; C:\poi-bin-5.4.1\lib\commons-math3-3.6.1.jar; C:\poi-bin-5.4.1\lib\commons-io-2.18.0.jar; C:\poi-bin-5.4.1\lib\SparseBitSet-1.3.jar; C:\poi-bin-5.4.1\lib\log4j-api-2.24.3.jar; C:\poi-bin-5.4.1\poi-ooxml-5.4.1.jar; C:\poi-bin-5.4.1\poi-ooxml-full-5.4.1.jar; C:\poi-bin-5.4.1\ooxml-lib\xmlbeans-5.3.0.jar C:\poi-bin-5.4.1\ooxml-lib\commons-compress-1.27.1.jar C:\poi-bin-5.4.1\ooxml-lib\curvesapi-1.08.jar C:\poi-bin-5.4.1\ooxml-lib\commons-lang3-3.16.0.jar |
2 |
Linux Export CLASSPATH = $CLASSPATH: /usr/share/poi-bin-5.4.1/poi-5.4.1.jar; /usr/share/poi-bin-5.4.1/lib/commons-codec-1.18.0.jar; /usr/share/poi-bin-5.4.1/lib/commons-collections4-4.4.jar; /usr/share/poi-bin-5.4.1/lib/commons-math3-3.6.1.jar; /usr/share/poi-bin-5.4.1/lib/commons-io-2.18.0.jar; /usr/share/poi-bin-5.4.1/lib/SparseBitSet-1.3.jar; /usr/share/poi-bin-5.4.1/lib/log4j-api-2.24.3.jar; /usr/share/poi-bin-5.4.1/poi-ooxml-5.4.1.jar; /usr/share/poi-bin-5.4.1/poi-ooxml-full-5.4.1.jar; /usr/share/poi-bin-5.4.1/ooxml-lib/xmlbeans-5.3.0.jar /usr/share/poi-bin-5.4.1/ooxml-lib/commons-compress-1.27.1.jar /usr/share/poi-bin-5.4.1/ooxml-lib/curvesapi-1.08.jar /usr/share/poi-bin-5.4.1/ooxml-lib/commons-lang3-3.16.0.jar |
pom.xml
Following is the pom.xml file to run the programs in this tutorial.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ApachePoiWord</groupId> <artifactId>ApachePoiWord</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>19</source> <target>19</target> <compilerArgs> <arg>--add-modules</arg> <arg>java.se,java.desktop</arg> </compilerArgs> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.4.1</version> </dependency> </dependencies> </project>
Apache POI Word - Core Classes
This chapter takes you through the classes and methods of Apache POI for managing a Word document.
Document
This is a marker interface (interface do not contain any methods), that notifies that the implemented class can be able to create a word document.
XWPFDocument
This is a class under org.apache.poi.xwpf.usermodel package. It is used to create MS-Word Document with .docx file format.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
commit() Commits and saves the document. |
2 |
createParagraph() Appends a new paragraph to this document. |
3 |
createTable() Creates an empty table with one row and one column as default. |
4 |
createTOC() Creates a table of content for Word document. |
5 |
getParagraphs() Returns the paragraph(s) that holds the text of the header or footer. |
6 |
getStyle() Returns the styles object used. |
For the remaining methods of this class, refer the complete API document at −
Package org.apache.poi.openxml4j.opc.internal.
XWPFParagraph
This is a class under org.apache.poi.xwpf.usermodel package and is used to create paragraph in a word document. This instance is also used to add all types of elements into word document.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
createRun() Appends a new run to this paragraph. |
2 |
getAlignment() Returns the paragraph alignment which shall be applied to the text in this paragraph. |
3 |
setAlignment(ParagraphAlignment align) Specifies the paragraph alignment which shall be applied to the text in this paragraph. |
4 |
setBorderBottom(Borders border) Specifies the border which shall be displayed below a set of paragraphs, which have the same set of paragraph border settings. |
5 |
setBorderLeft(Borders border) Specifies the border which shall be displayed on the left side of the page around the specified paragraph. |
6 |
setBorderRight(Borders border) Specifies the border which shall be displayed on the right side of the page around the specified paragraph. |
7 |
setBorderTop(Borders border) Specifies the border which shall be displayed above a set of paragraphs which have the same set of paragraph border settings. |
For the remaining methods of this class, refer the complete API document at −
XWPFRun
This is a class under org.apache.poi.xwpf.usermodel package and is used to add a region of text to the paragraph.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
addBreak() Specifies that a break shall be placed at the current location in the run content. |
2 |
addTab() Specifies that a tab shall be placed at the current location in the run content. |
3 |
setColor(java.lang.String rgbStr) Sets text color. |
4 |
setFontSize(int size) Specifies the font size which shall be applied to all noncomplex script characters in the content of this run when displayed. |
5 |
setText(java.lang.String value) Sets the text of this text run. |
6 |
setBold(boolean value) Specifies whether the bold property shall be applied to all non-complex script characters in the content of this run when displayed in a document. |
For the remaining methods of this class, refer the complete API document at −
XWPFStyle
This is a class under org.apache.poi.xwpf.usermodel package and is used to add different styles to the object elements in a word document.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
getNextStyleID() It is used to get StyleID of the next style. |
2 |
getStyleId() It is used to get StyleID of the style. |
3 |
getStyles() It is used to get styles. |
4 |
setStyleId(java.lang.String styleId) It is used to set styleID. |
For the remaining methods of this class, refer the complete API document at −
XWPFTable
This is a class under org.apache.poi.xwpf.usermodel package and is used to add table data into a word document.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
addNewCol() Adds a new column for each row in this table. |
2 |
addRow(XWPFTableRow row, int pos) Adds a new Row to the table at position pos. |
3 |
createRow() Creates a new XWPFTableRow object with as many cells as the number of columns defined in that moment. |
4 |
setWidth(int width) Sets the width of the column. |
For the remaining methods of this class, refer the complete API document at: POI API Documentation
XWPFWordExtractor
This is a class under org.apache.poi.xwpf.extractor package. It is a basic parser class used to extract the simple text from a Word document.
Class Methods
Sr.No. | Method & Description |
---|---|
1 |
getText() Retrieves all the text from the document. |
For the remaining methods of this class, refer the complete API document at: POI API Documentation
Apache POI Word - Document
Here the term 'document' refers to a MS-Word file. After completion of this chapter, you will be able to create new documents and open existing documents using your Java program.
Create a Document
First of all, let us create a Document using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can save to disk.
The following code snippet is used to create a document −
//Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream( new File("example.docx")); document.write(out);
Example - Creating a Blank Document
The following simple program is used to create a blank MS-Word document −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream( new File("example.docx")); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
If your system environment is configured with the POI library, it will compile and execute to generate a blank Word document file named example.docx in your current directory and display the following output in the command prompt −
example.docx written successfully
Apache POI Word - Paragraph
In this chapter you will learn how to create a Paragraph and how to add it to a document using Java. Paragraph is a part of a page in a Word file.
After completing this chapter, you will be able to create a Paragraph and perform read operations on it.
Create a Paragraph
First of all, let us create a Paragraph using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can create a Paragraph.
The following code snippet is used to create a paragraph −
//Create Blank document XWPFDocument document = new XWPFDocument(); //Create a blank spreadsheet XWPFParagraph paragraph = document.createParagraph();
Running a Paragraph
You can enter the text or any object element, using Run. Using Paragraph instance you can create run.
The following code snippet is used to create a Run.
XWPFRun run = paragraph.createRun();
Example - Write into a Paragraph of a Document
Let us try entering some text into a document. Consider the below text data −
At tutorialspoint.com, we strive hard to provide quality tutorials for self-learning purpose in the domains of Academics, Information Technology, Management and Computer Programming Languages.
The following code is used to write the above data into a paragraph.
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create Paragraph XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning " + "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming Languages."); document.write(out); out.close(); document.close(); System.out.println("example.docx written successfully"); } }
Output
It will compile and execute to generate a Word file named createparagraph.docx in your current directory and you will get the following output in the command prompt −
example.docx written successfully
The example.docx file looks as follows.

Apache POI Word - Borders
In this chapter, you will learn how to apply border to a paragraph using Java programming.
Create a Border
First of all, let us create a Paragraph using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can create a Paragraph and apply the borders accordingly.
The following code snippet is used to set a border −
//Create Blank document XWPFDocument document = new XWPFDocument(); //Create a blank spreadsheet XWPFParagraph paragraph = document.createParagraph(); //Set bottom border to paragraph paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
Example - Applying Borders to Paragraph in a document
The following code is used to apply Borders in a Document −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.Borders; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set bottom border to paragraph paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES); //Set left border to paragraph paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES); //Set right border to paragraph paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES); //Set top border to paragraph paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES); XWPFRun run = paragraph.createRun(); run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning " + "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming " + "Languages."); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
If your system is configured with the POI library, then it will compile and execute to generate a Word document named example.docx in your current directory and display the following output −
example.docx written successfully
The example.docx file looks as follows −

Apache POI Word - Borders
In this chapter, you will learn how to apply border to a paragraph using Java programming.
Create a Border
First of all, let us create a Paragraph using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can create a Paragraph and apply the borders accordingly.
The following code snippet is used to set a border −
//Create Blank document XWPFDocument document = new XWPFDocument(); //Create a blank spreadsheet XWPFParagraph paragraph = document.createParagraph(); //Set bottom border to paragraph paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
Example - Applying Borders to Paragraph in a document
The following code is used to apply Borders in a Document −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.Borders; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set bottom border to paragraph paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES); //Set left border to paragraph paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES); //Set right border to paragraph paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES); //Set top border to paragraph paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES); XWPFRun run = paragraph.createRun(); run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning " + "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming " + "Languages."); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
If your system is configured with the POI library, then it will compile and execute to generate a Word document named example.docx in your current directory and display the following output −
example.docx written successfully
The example.docx file looks as follows −

Apache POI Word - Tables
In this chapter, you will learn how to create a table of data in a document. You can create a table data by using XWPFTable class. By adding each Row to table and adding each cell to Row, you will get table data.
Create a Table
First of all, let us create a Table using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can create a table.
The following code snippet is used to create and populate a table −
//Create Blank document XWPFDocument document = new XWPFDocument(); //create table XWPFTable table = document.createTable(); //create first row XWPFTableRow tableRowOne = table.getRow(0); //set data in column one tableRowOne.getCell(0).setText("col one, row one");
Example - Create Table in a Document
The following code is used to creating table in a document −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create table XWPFTable table = document.createTable(); //create first row XWPFTableRow tableRowOne = table.getRow(0); tableRowOne.getCell(0).setText("col one, row one"); tableRowOne.addNewTableCell().setText("col two, row one"); tableRowOne.addNewTableCell().setText("col three, row one"); //create second row XWPFTableRow tableRowTwo = table.createRow(); tableRowTwo.getCell(0).setText("col one, row two"); tableRowTwo.getCell(1).setText("col two, row two"); tableRowTwo.getCell(2).setText("col three, row two"); //create third row XWPFTableRow tableRowThree = table.createRow(); tableRowThree.getCell(0).setText("col one, row three"); tableRowThree.getCell(1).setText("col two, row three"); tableRowThree.getCell(2).setText("col three, row three"); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
It generates a Word file named example.docx in your current directory and display the following output on the command prompt −
example.docx written successfully
The example.docx file looks as follows −

Apache POI Word - Font & Alignment
This chapter shows how to apply different font styles and alignments in a Word document using Java. Generally, Font Style contains: Font size, Type, Bold, Italic, and Underline. And Alignment is categorized into left, center, right, and justify.
Example - Applying Font Styles in a Document
The following code is used to set different styles of font −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.VerticalAlign; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream(new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set Bold an Italic XWPFRun paragraphOneRunOne = paragraph.createRun(); paragraphOneRunOne.setBold(true); paragraphOneRunOne.setItalic(true); paragraphOneRunOne.setText("Font Style"); paragraphOneRunOne.addBreak(); //Set text Position XWPFRun paragraphOneRunTwo = paragraph.createRun(); paragraphOneRunTwo.setText("Font Style two"); paragraphOneRunTwo.setTextPosition(100); //Set Strike through and Font Size and Subscript XWPFRun paragraphOneRunThree = paragraph.createRun(); paragraphOneRunThree.setStrike(true); paragraphOneRunThree.setFontSize(20); paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT); paragraphOneRunThree.setText(" Different Font Styles"); document.write(out); out.close(); document.close(); System.out.println("example.docx written successully"); } }
Output
It will generate a Word file named example.docx in your current directory and display the following output on the command prompt −
example.docx written successfully
The fontstyle.docx file looks as follows.

Example - Setting Alignment to Paragraph Text of a Document
The following code is used to set alignment to the paragraph text −
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.File; import java.io.FileOutputStream; 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; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { //Blank Document XWPFDocument document = new XWPFDocument(); //Write the Document in file system FileOutputStream out = new FileOutputStream( new File("example.docx")); //create paragraph XWPFParagraph paragraph = document.createParagraph(); //Set alignment paragraph to RIGHT paragraph.setAlignment(ParagraphAlignment.RIGHT); XWPFRun run = paragraph.createRun(); run.setText("At tutorialspoint.com, we strive hard to " + "provide quality tutorials for self-learning " + "purpose in the domains of Academics, Information " + "Technology, Management and Computer Programming " + "Languages."); //Create Another paragraph paragraph = document.createParagraph(); //Set alignment paragraph to CENTER paragraph.setAlignment(ParagraphAlignment.CENTER); run = paragraph.createRun(); run.setText("The endeavour started by Mohtashim, an AMU " + "alumni, who is the founder and the managing director " + "of Tutorials Point (I) Pvt. Ltd. He came up with the " + "website tutorialspoint.com in year 2006 with the help" + "of handpicked freelancers, with an array of tutorials" + " for computer programming languages. "); document.write(out); out.close(); document.close(); System.out.println("example.docx written successfully"); } }
Output
It will generate a Word file named example.docx in your current directory and display the following output in the command prompt −
example.docx written successfully
The example.docx file looks as follows −

Apache POI Word - Text Extraction
This chapter explains how to extract simple text data from a Word document using Java. In case you want to extract metadata from a Word document, make use of Apache Tika.
For .docx files, we use the class org.apache.poi.xwpf.extractor.XPFFWordExtractor that extracts and returns simple data from a Word file. In the same way, we have different methodologies to extract headings, footnotes, table data, etc. from a Word file.
The following code shows how to extract simple text from a Word file −
// create a document object from existing work document XWPFDocument docx = new XWPFDocument(new FileInputStream("example.docx")); // using XWPFWordExtractor Class XWPFWordExtractor we = new XWPFWordExtractor(docx); // extract the text System.out.println(we.getText());
Example - Extracting Text from a Document
ApachePoiDocDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import org.apache.poi.xwpf.extractor.XWPFWordExtractor; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class ApachePoiDocDemo { public static void main(String[] args)throws Exception { XWPFDocument docx = new XWPFDocument(new FileInputStream("example.docx")); //using XWPFWordExtractor Class XWPFWordExtractor we = new XWPFWordExtractor(docx); System.out.println(we.getText()); we.close(); } }
Output
It will generate the following output −
At tutorialspoint.com, we strive hard to provide quality tutorials for self-learning purpose in the domains of Academics, Information Technology, Management and Computer Programming Languages. The endeavour started by Mohtashim, an AMU alumni, who is the founder and the managing director of Tutorials Point (I) Pvt. Ltd. He came up with the website tutorialspoint.com in year 2006 with the helpof handpicked freelancers, with an array of tutorials for computer programming languages.