Apache poi for word


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 takes you through the process of setting up Apache POI on Windows and Linux based systems. Apache POI can be easily installed and integrated with your current Java environment following a few simple steps without any complex setup procedures. User administration is required while installation.

System Requirements

JDK Java SE 2 JDK 1.5 or above
Memory 1 GB RAM (recommended)
Disk Space No minimum requirement
Operating System Version Windows XP or above, Linux

Let us now proceed with the steps to install Apache POI.

Step 1: Verify your Java Installation

First of all, you need to have Java Software Development Kit (SDK) installed on your system. To verify this, execute any of the two commands depending on the platform you are working on.

If the Java installation has been done properly, then it will display the current version and specification of your Java installation. A sample output is given in the following table.

Platform Command Sample Output
Windows

Open command console and type −

>java -version

java version «11.0.11» 2021-04-20 LTS

Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)

Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)

Linux

Open command terminal and type −

$java -version

java version «11.0.11» 2021-04-20 LTS

Open JDK Runtime Environment 18.9 (build 11.0.11+9-LTS-194)

Open JDK 64-Bit Server VM (build 11.0.11+9-LTS-194, mixed mode)

  • We assume the readers of this tutorial have Java SDK version 11.0.11 installed on their system.

  • In case you do not have Java SDK, download its current version from www.oracle.com/technetwork/java/javase/downloads/index.html and have it installed.

Step 2: Set your Java Environment

Set the environment variable JAVA_HOME to point to the base directory location where Java is installed on your machine. For example,

Sr.No. Platform & Description
1

Windows

Set JAVA_HOME to C:ProgramFilesjavajdk11.0.11

2

Linux

Export JAVA_HOME = /usr/local/java-current

Append the full path of Java compiler location to the System Path.

Sr.No. Platform & Description
1

Windows

Append the String «C:Program FilesJavajdk11.0.11bin» to the end of the system variable PATH.

2

Linux

Export PATH = $PATH:$JAVA_HOME/bin/

Execute the command java -version from the command prompt as explained above.

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.1.0poi-5.1.0.jar;

C:poi-bin-5.1.0poi-ooxml-5.1.0.jar;

C:poi-bin-5.1.0poi-ooxml-full-5.1.0.jar;

C:poi-bin-5.1.0libcommons-codec-1.15.jar;

C:poi-bin-5.1.0libcommons-collections4-4.4.jar;

C:poi-bin-5.1.0libcommons-io-2.11.0.jar;

C:poi-bin-5.1.0libcommons-math3-3.6.1.jar;

C:poi-bin-5.1.0liblog4j-api-2.14.1.jar;

C:poi-bin-5.1.0libSparseBitSet-1.2.jar;

Cpoi-bin-5.1.0ooxml-libcommons-compress-1.21.jar

Cpoi-bin-5.1.0ooxml-libcommons-logging-1.2.jar

Cpoi-bin-5.1.0ooxml-libcurvesapi-1.06.jar

Cpoi-bin-5.1.0ooxml-libslf4j-api-1.7.32.jar

Cpoi-bin-5.1.0ooxml-libxmlbeans-5.0.2.jar

2

Linux

Export CLASSPATH = $CLASSPATH:

/usr/share/poi-bin-5.1.0/poi-5.1.0.jar.tar:

/usr/share/poi-bin-5.1.0/poi-ooxml-5.1.0.tar:

/usr/share/poi-bin-5.1.0/poi-ooxml-full-5.1.0.tar:

/usr/share/poi-bin-5.1.0/lib/commons-codec-1.15.jar.tar:

/usr/share/poi-bin-5.1.0/lib/commons-collections4-4.4.tar:

/usr/share/poi-bin-5.1.0/lib/commons-io-2.11.0.tar:

/usr/share/poi-bin-5.1.0/lib/commons-math3-3.6.1.tar:

/usr/share/poi-bin-5.1.0/lib/log4j-api-2.14.1.tar:

/usr/share/poi-bin-5.1.0/lib/SparseBitSet-1.2.tar:

/usr/share/poi-bin-5.1.0/ooxml-lib/commons-compress-1.21.tar:

/usr/share/poi-bin-5.1.0/ooxml-lib/commons-logging-1.2.tar:

/usr/share/poi-bin-5.1.0/ooxml-lib/curvesapi-1.06.tar:

/usr/share/poi-bin-5.1.0/ooxml-lib/slf4j-api-1.7.32.tar:

/usr/share/poi-bin-5.1.0/ooxml-lib/xmlbeans-5.0.2.tar:

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 −

POI API Documentation

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 −

POI API Documentation

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 −

POI API Documentation

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 Blank Document

The following simple program is used to create a blank MS-Word document −

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class CreateDocument {
   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("createdocument.docx"));
      document.write(out);
      out.close();
      System.out.println("createdocument.docx written successully");
   }
}

Save the above Java code as CreateDocument.java, and then compile and execute it from the command prompt as follows −

$javac  CreateDocument.java
$java CreateDocument

If your system environment is configured with the POI library, it will compile and execute to generate a blank Word document file named createdocument.docx in your current directory and display the following output in the command prompt −

createdocument.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 spreadsheet −

//Create Blank document
XWPFDocument document = new XWPFDocument();

//Create a blank spreadsheet
XWPFParagraph paragraph = document.createParagraph();

Run on 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();

Write into a Paragraph

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.

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 CreateParagraph {
   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("createparagraph.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();
      System.out.println("createparagraph.docx written successfully");
   }
}

Save the above Java code as CreateParagraph.java, and then compile and run it from the command prompt as follows −

$javac CreateParagraph.java
$java CreateParagraph

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 −

createparagraph.docx written successfully

The createparagraph.docx file looks as follows.

Create Paragraph

Apache POI Word — Borders

In this chapter, you will learn how to apply border to a paragraph using Java programming.

Applying Border

The following code is used to apply Borders in a Document −

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 ApplyingBorder {

   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("applyingborder.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();
      System.out.println("applyingborder.docx written successully");
   }
}

Save the above code in a file named ApplyingBorder.java, compile and execute it from the command prompt as follows −

$javac ApplyingBorder.java
$java ApplyingBorder

If your system is configured with the POI library, then it will compile and execute to generate a Word document named applyingborder.docx in your current directory and display the following output −

applyingborder.docx written successfully

The applyingborder.docx file looks as follows −

Border Paragraph

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 Table

The following code is used to creating table in a document −

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 CreateTable {

   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("create_table.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();
      System.out.println("create_table.docx written successully");
   }
}

Save the above code in a file named CreateTable.java. Compile and execute it from the command prompt as follows −

$javac CreateTable.java
$java CreateTable

It generates a Word file named createtable.docx in your current directory and display the following output on the command prompt −

createtable.docx written successfully

The createtable.docx file looks as follows −

Create Table

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.

Font Style

The following code is used to set different styles of font −

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 FontStyle {

   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("fontstyle.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();
      System.out.println("fontstyle.docx written successully");
   }
}

Save the above code as FontStyle.java and then compile and execute it from the command prompt as follows −

$javac FontStyle.java
$java FontStyle

It will generate a Word file named fontstyle.docx in your current directory and display the following output on the command prompt −

fontstyle.docx written successfully

The fontstyle.docx file looks as follows.

Font Style

Alignment

The following code is used to set alignment to the paragraph text −

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 AlignParagraph {

   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("alignparagraph.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();
      System.out.println("alignparagraph.docx written successfully");
   }
}

Save the above code as AlignParagraph.java and then compile and execute it from the command prompt as follows −

$javac AlignParagraph.java
$java AlignParagraph

It will generate a Word file named alignparagraph.docx in your current directory and display the following output in the command prompt −

alignparagraph.docx written successfully

The alignparagraph.docx file looks as follows −

Align Paragraph

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 −

import java.io.FileInputStream;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordExtractor {
   public static void main(String[] args)throws Exception {
      XWPFDocument docx = new XWPFDocument(new FileInputStream("createparagraph.docx"));
      
      //using XWPFWordExtractor Class
      XWPFWordExtractor we = new XWPFWordExtractor(docx);
      System.out.println(we.getText());
   }
}

Save the above code as WordExtractor.java. Compile and execute it from the command prompt as follows −

$javac WordExtractor.java
$java WordExtractor

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.

In this tutorial I will show you how to create a word document using apache poi or write to a word document using apache poi API. Microsoft word document is a great tool to document your stuff. This example is tested on both 3.15 and 4.1.1 versions of Apache POI library. I am also going to show you how to use build tools maven and gradle for the application.

We will create here a Java application to create word document using apache poi library. Using apache poi library is very easy for any kind of activities in word document.

Prerequisites

Eclipse 4.12, JDK 1.8 or 12, Maven 3.6.1, Gradle 5.6, Apache POI 3.15 to 4.1.1

Create Project

You basically need to create a maven or gradle project in eclipse. The name of the project is word-apache-poi.

Once the project is created and eclipse finishes build then you need to modify the pom.xml file as shown below.

Here we will add apache poi API as a dependency for working with microsoft word document or even you can work with open source word document.

<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>com.roytuts</groupId>
	<artifactId>word-apache-poi</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<poi.version>3.15</poi.version>
	</properties>
	
	<dependencies>
		<!-- apache poi for xlsx, docx etc reading/writing -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>${poi.version}</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

If you want to use gradle build script to create your project then use below build script to setup the project. Here also we have used the same apache poi version as you have seen in the above maven based pom.xml file.

plugins {
    id 'java-library'
}

sourceCompatibility = 12 or 1.8
targetCompatibility = 12 or 1.8

repositories {
    jcenter()
}

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.1 or 3.15'
}

Create Word File

Create below class to create WordDocx.docx file.

The below source code creates a word document and writes sample texts to different paragraphs in different formats.

The first paragraph writes texts with justify alignments. It also adds first line with indent.

The second paragraph writes test with left alignment. It also adds first line with indent.

The third paragraph texts are italic with left alignment. The fourth paragraph texts are bold with left alignment.

The fifth paragraph texts are strike-through with left alignment. The sixth paragraph texts are undelined with left alignment.

Finally we create the output WordDocx.docx file in the root directory of the project. You can also save the file anywhere you want. This will create a word document using apache poi.

package com.roytuts.word.apache.poi;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class WordDocx {

	public static void main(String[] args) {
		createWordDocument("WordDocx.docx");
	}

	public static void createWordDocument(final String outputFileName) {
		// create a document
		XWPFDocument doc = new XWPFDocument();

		// create a paragraph with justify alignment
		XWPFParagraph p1 = doc.createParagraph();

		// first line indentation in the paragraph
		p1.setFirstLineIndent(400);

		// justify alignment
		p1.setAlignment(ParagraphAlignment.DISTRIBUTE);

		// wrap words
		p1.setWordWrapped(true);

		// insert page break after this paragraph
		// p1.setPageBreak(true);
		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r1 = p1.createRun();
		String t1 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog."
				+ " Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r1.setText(t1);

		// create a paragraph with left alignment
		XWPFParagraph p2 = doc.createParagraph();

		// first line indentation in the paragraph
		p2.setFirstLineIndent(400);

		// left alignment
		p2.setAlignment(ParagraphAlignment.LEFT);

		// wrap words
		p2.setWordWrapped(true);

		// insert page break after this paragraph
		// p2.setPageBreak(true);
		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r2 = p2.createRun();
		String t2 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog."
				+ " Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r2.setText(t2);

		// create a paragraph with ITALIC text
		XWPFParagraph p3 = doc.createParagraph();

		// left alignment
		p3.setAlignment(ParagraphAlignment.LEFT);

		// wrap words
		p3.setWordWrapped(true);

		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r3 = p3.createRun();
		String t3 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r3.setText(t3);

		// make text italic
		r3.setItalic(true);

		// create a paragraph with BOLD text
		XWPFParagraph p4 = doc.createParagraph();

		// left alignment
		p4.setAlignment(ParagraphAlignment.LEFT);

		// wrap words
		p4.setWordWrapped(true);

		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r4 = p4.createRun();
		String t4 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r4.setText(t4);

		// make text bold
		r4.setBold(true);

		// create a paragraph with Strike-Through text
		XWPFParagraph p5 = doc.createParagraph();

		// left alignment
		p5.setAlignment(ParagraphAlignment.LEFT);

		// wrap words
		p5.setWordWrapped(true);

		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r5 = p5.createRun();
		String t5 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r5.setText(t5);

		// make StrikeThrough
		r5.setStrikeThrough(true);

		// create a paragraph with Underlined text
		XWPFParagraph p6 = doc.createParagraph();

		// left alignment
		p6.setAlignment(ParagraphAlignment.LEFT);

		// wrap words
		p6.setWordWrapped(true);

		// XWPFRun object defines a region of text with a common set of
		// properties
		XWPFRun r6 = p6.createRun();
		String t6 = "Sample Paragraph Post. This is a sample Paragraph post. Sample Paragraph text is being cut and pasted again and again. This is a sample Paragraph post. peru-duellmans-poison-dart-frog.";
		r6.setText(t6);

		// make Underlined
		r6.setUnderline(UnderlinePatterns.SINGLE);

		// write to a docx file
		FileOutputStream fo = null;
		try {
			// create .docx file
			fo = new FileOutputStream(outputFileName);

			// write to the .docx file
			doc.write(fo);
		} catch (IOException e) {
		} finally {
			if (fo != null) {
				try {
					fo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (doc != null) {
				try {
					doc.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

Related Posts:

  • add images to word document using apache poi
  • create table in word document using apache poi
  • create header and footer in word document using apache poi

Testing the Application

Run the above class to see the output in WordDocx.docx file. The file is created under root directory of the project.

Now open the created word document you should see the expected output similar to below image.

Hope you have got an idea how to create a word document using apache poi.

Source Code

Download

Thanks for reading.

package ua.com.prologistic;

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;

import java.io.FileOutputStream;

public class WordWorker {

    public static void main(String[] args) {

        try {

            // создаем модель docx документа,

            // к которой будем прикручивать наполнение (колонтитулы, текст)

            XWPFDocument docxModel = new XWPFDocument();

            CTSectPr ctSectPr = docxModel.getDocument().getBody().addNewSectPr();

            // получаем экземпляр XWPFHeaderFooterPolicy для работы с колонтитулами

            XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(docxModel, ctSectPr);

            // создаем верхний колонтитул Word файла

            CTP ctpHeaderModel = createHeaderModel(

            «Верхний колонтитул — создано с помощью Apache POI на Java :)»

            );

            // устанавливаем сформированный верхний

            // колонтитул в модель документа Word

            XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeaderModel, docxModel);

            headerFooterPolicy.createHeader(

                    XWPFHeaderFooterPolicy.DEFAULT,

                    new XWPFParagraph[]{headerParagraph}

            );

            // создаем нижний колонтитул docx файла

            CTP ctpFooterModel = createFooterModel(«Просто нижний колонтитул»);

            // устанавливаем сформированый нижний

            // колонтитул в модель документа Word

            XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooterModel, docxModel);

            headerFooterPolicy.createFooter(

                    XWPFHeaderFooterPolicy.DEFAULT,

                    new XWPFParagraph[]{footerParagraph}

            );

            // создаем обычный параграф, который будет расположен слева,

            // будет синим курсивом со шрифтом 25 размера

            XWPFParagraph bodyParagraph = docxModel.createParagraph();

            bodyParagraph.setAlignment(ParagraphAlignment.RIGHT);

            XWPFRun paragraphConfig = bodyParagraph.createRun();

            paragraphConfig.setItalic(true);

            paragraphConfig.setFontSize(25);

            // HEX цвет без решетки #

            paragraphConfig.setColor(«06357a»);

            paragraphConfig.setText(

            «Prologistic.com.ua — новые статьи по Java и Android каждую неделю. Подписывайтесь!»

            );

            // сохраняем модель docx документа в файл

            FileOutputStream outputStream = new FileOutputStream(«F:/Apache POI Word Test.docx»);

            docxModel.write(outputStream);

            outputStream.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

        System.out.println(«Успешно записан в файл»);

    }

    private static CTP createFooterModel(String footerContent) {

        // создаем футер или нижний колонтитул

        CTP ctpFooterModel = CTP.Factory.newInstance();

        CTR ctrFooterModel = ctpFooterModel.addNewR();

        CTText cttFooter = ctrFooterModel.addNewT();

        cttFooter.setStringValue(footerContent);

        return ctpFooterModel;

    }

    private static CTP createHeaderModel(String headerContent) {

        // создаем хедер или верхний колонтитул

        CTP ctpHeaderModel = CTP.Factory.newInstance();

        CTR ctrHeaderModel = ctpHeaderModel.addNewR();

        CTText cttHeader = ctrHeaderModel.addNewT();

        cttHeader.setStringValue(headerContent);

        return ctpHeaderModel;

    }

}

Like this post? Please share to your friends:
  • Apache poi excel to pdf
  • Apache poi excel read
  • Apache poi excel maven
  • Apache poi excel kotlin
  • Apache poi excel format