Java Program to Create a Word Document
Last Updated :
24 May, 2022
A Word file is supposed to be created without Word with the help of Java application protocol interfaces.
Concepts Involved: Apache POI and Maven
Apache POI Is an API provided by Apache foundation which is a collection of different java libraries. This facility gives the library to read, write, and manipulate different Microsoft files such as Excel sheets, PowerPoint, and Word files. There are two types basically older version including '.doc', '.ppt' while newer versions of files as '.docx', '.pptx'. There are two ways to deal with Apache POI as mentioned below:
Binary Distribution | Source Distribution |
---|
No compilation is required to generate file.jar files | Files to generated needs to be compiled in order to generate file.jar files |
If working with Linux go for the '.tar.gz' version (version 3.5 backward) otherwise simply download the .zip. the file extension for installation to work on Apache POI. | Here format does not matter because file is not compiled so can be altered as per need |
Here zip file is taken for consideration and also if the operating system is Windows, zip files should be preferred. It is a simple java project so binary distribution API is used. In the process of creating the document, several paragraphs will be inserted as a sample to display output and will be providing styles to paragraphs such as font color, font name, and font size.
Now in order to create a Word file without using Microsoft Word, there is a java interface called Spire and if there is a need to create a PDF document without using Adobe Acrobat then it can be done with the use of an interface known as 'E-Ice blue'. Here 'Spire.doc' must have to be imported as per problem statement as all dealing is in word format.
Spire.Doc for Java is a professional Java Word API that enables Java applications to create, convert, manipulate, and print Word documents without using Microsoft Office. It will be imported as a reference for this program as a reference.
Syntax: For importing libraries in java of Spire
import Spire.Doc.jar ;
Components Of Spire involved are as follows:
Component | Responsibility |
---|
XML Word Processor Format (XWPF) | Reading and writing in 'docx' extensions files of MS-Word |
Horrible Word Processor Format (HWPF) | Reading and writing in 'doc' extensions files of MS-Word |
Additionally, there is another Java API for PDF format as discussed above 'E-Ice Blue' that enables developers to read, write, convert, and print PDF documents in Java applications without using Adobe Acrobat. Similarly, a PowerPoint API allows developers to create, read, edit, convert, and print PowerPoint files within Java applications.
If user is creating shear Maven project, one can easily add the jar dependency by adding the following configurations to the pom.xml.
Now, just like any class Apache POI contains classes and methods to work on. Major components of Apache POI are discussed below to understand the internal working of a file, how it is been generated without Word with help of classes and methods. There are basically two versions of files been present in Word itself.

Older files extensions | Newer file extension (Version 3.5) |
---|
doc | docx |
xls | xlsx |
ppt | pptx |
Maven
These files are accessed using Maven. Maven is a powerful project management tool that is based on POM (project object model). It is used for projects to build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT.
In short terms we can tell maven is a tool that can be used for building and managing any Java-based project. Maven makes the day-to-day work of Java developers easier and generally help with the comprehension of any Java-based project.
Maven does have some specific commands to deal with files. Most frequently used are:
poi + poi + scratchpadpoi // In order to deal with older Word file versions
poi + poi-ooxml // In order to deal with new Word file versions
Implementation: Below program depicts the responsibility of Spire API in creating a Word file:
Java
// Java program to create a Word document
// Importing Spire Word libraries
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BuiltinStyle;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
class GFG {
// Main driver method
public static void main(String[] args)
{
// create a Word document
Document document = new Document();
// Add a section
Section section = document.addSection();
// Add a heading
Paragraph heading = section.addParagraph();
heading.appendText("Java");
// Add a subheading
Paragraph subheading_1 = section.addParagraph();
subheading_1.appendText("What's Java");
// Adding sub-headings
// two paragraph under the first subheading
// Adding paragraph 1
Paragraph para_1 = section.addParagraph();
para_1.appendText(
"Java is a general purpose, high-level programming language developed by Sun Microsystems."
+ " The Java programming language was developed by a small team of engineers, "
+ "known as the Green Team, who initiated the language in 1991.");
// Adding paragraph 2
Paragraph para_2 = section.addParagraph();
para_2.appendText(
"Originally called OAK, the Java language was designed for handheld devices and set-top boxes. "
+ "Oak was unsuccessful and in 1995 Sun changed the name to Java and modified the language to take "
+ "advantage of the burgeoning World Wide Web. ");
// Adding another subheading
Paragraph subheading_2 = section.addParagraph();
subheading_2.appendText("Java Today");
// Adding one paragraph under the second subheading
Paragraph para_3 = section.addParagraph();
para_3.appendText(
"Today the Java platform is a commonly used foundation for developing and delivering content "
+ "on the web. According to Oracle, there are more than 9 million Java developers worldwide and more "
+ "than 3 billion mobile phones run Java.");
// Apply built-in style to heading and subheadings
// so that it is easily distinguishable
heading.applyStyle(BuiltinStyle.Title);
subheading_1.applyStyle(BuiltinStyle.Heading_3);
subheading_2.applyStyle(BuiltinStyle.Heading_3);
// Customize a paragraph style
ParagraphStyle style = new ParagraphStyle(document);
// Paragraph name
style.setName("paraStyle");
// Paragraph format
style.getCharacterFormat().setFontName("Arial");
// Paragraph font size
style.getCharacterFormat().setFontSize(11f);
// Adding styles using inbuilt method
document.getStyles().add(style);
// Apply the style to other paragraphs
para_1.applyStyle("paraStyle");
para_2.applyStyle("paraStyle");
para_3.applyStyle("paraStyle");
// Iteration for white spaces
for (int i = 0;
i < section.getParagraphs().getCount(); i++) {
// Automatically add whitespaces
// to every paragraph in the file
section.getParagraphs()
.get(i)
.getFormat()
.setAfterAutoSpacing(true);
}
// Save the document
document.saveToFile(
"output/CreateAWordDocument.docx",
FileFormat.Docx);
}
}
Output:

Similar Reads
Java Program to Create a Blank PPT Document
Program to create a blank PPT document using Java. The external jar(Java archive) file is required for the creation of a new PPT document. Below is the implementation for the same. An object of XMLSlideShow class inside External Apache POI module is required for the creation of a new PPT. Algorithm:
1 min read
Java Program to Add Tables to a Word Document
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision. It has C++, Python, Java, and MATLAB int
3 min read
Java Program to Write a Paragraph in a Word Document
Java provides us various packages in-built into the environment, which facilitate the ease of reading, writing, and modifying the documents. The package org.apache.poi.xwpf.usermodel provides us the various features of formatting and appending the content in word documents. There are various classes
3 min read
Java Program to Create a New File
There are two standard methods to create a new file, either directly with the help of the File class or indirectly with the help of the FileOutputStream class by creating an object of the file in both approaches.Methods to Create Files in JavaThere are two methods mentioned belowUsing the File Class
4 min read
Java Program to Create a Temporary File
A File is an abstract path, it has no physical existence. It is only when "using" that File that the underlying physical storage is hit. When the file is getting created indirectly the abstract path is getting created. The file is one way, in which data will be stored as per requirement. Type of Fi
6 min read
Java Program to Create blank Excel Sheet
ApachePOI stands for poor obfuscation Implementation which is a Java API for reading and writing Microsoft documents. It contains classes and interfaces namely Wordbook, Sheet, Row, and Cell. Apache POI can be used to access 'xlsx' extension files and as well as 'xlsx' extension files Concept: Apach
3 min read
Java Program to Write into a File
FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:Using writeString(
5 min read
Java Program to Extract Content from a TXT document
Java class< file using the Apache Tika library is used. Â For document type detection and content extraction from various file formats, it uses various document parsers and document type detection techniques to detect and extract data. It provides a single generic API for parsing different file fo
3 min read
Java Program to Convert File to a Byte Array
Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea
3 min read
Java Program to Extract Paragraphs From a Word Document
The article demonstrates how to extract paragraphs from a word document using the getParagraphs() method of XWPFDocument class provided by the Apache POI package. Apache POI is a project developed and maintained by Apache Software Foundation that provides libraries to perform numerous operations on
2 min read