How to Create a Blank Word Document using Java?
Last Updated :
08 Dec, 2020
Apache POI and File stream is the root concept to create a Word document. 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:
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 ;
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.
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.

File Streams in Java itself is an abstract class so it has three classes InputStreamClass, OutputStreamClass, and ByteStreamClass to operational execution. When an I/O occurs through byte data is called Byte handling and when an I/O stream occurs with character stream then it is called file handling process with byte stream. Remember the base reference for the newer version of files is from Word version 3.5 onwards.

Approach:
- File Handling provides how to read from and write to a file in Java. Java provides the basic input/output package for reading and writing streams. java.io package allows doing all the input and output tasks in java. Further, in detail is explained below under file streams in java.
- Java contains an in-built package org.apache.poi.xwpf.usermodel which can be imported into the environment, providing a wide range of functionalities involved with documents. This package provides a class XWPFDocument which can be used to work with ‘.docx’ files. The other required package involves File for processing and working with files and FileOutputStream to establish a connection and create the corresponding file. It also stimulates the procedure of writing contents onto the specified file location. A blank document can be created and stored at the local storage using these packages. The connection needs to be closed after writing the contents.

Algorithm: To create a blank Word file
- Creating a blank document using file methods.
- Specifying the path or directory where the blank document is to be created.
- Writing to the document in the file stream.
- Writings contents to the document.
- Close the file connection.
Implementation: The following Java code illustrates this procedure: Here it is a simple Apache implementation program so no need to introduce Maven libraries to the program:
Java
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class GFG {
public static void main(String[] args) throws Exception
{
XWPFDocument xwpfdocument = new XWPFDocument();
File file = new File( "C:/blankdocument.docx" );
FileOutputStream ostream
= new FileOutputStream(file);
xwpfdocument.write(ostream);
ostream.close();
}
}
|
Output: The code creates a blank document file in the local directory as specified by the programmer in the code.

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
Adding Images to a Word Document using Java
Java makes it possible to add images to Word documents using the addPicture() method of XWPFRun class provided by Apache POI package. Apache POI is a popular API developed and maintained by the Apache Software Foundation. It provides several classes and methods to perform different file operations o
3 min read
Java Program to Create a Word Document
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
6 min read
How to Apply Borders to the Text in a Word Document using Java?
Java Library Apache POI will be used to apply borders to text in a Word Document in java. Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project provides pure Java libraries for reading and writing files in Microsoft Office formats, such as
3 min read
Creating an Empty PDF Document using Java
For creating a PDF document using Java, we need to know the packages/libraries which are necessary for writing the code. So, for creating a PDF doc, we will be using the iText 7 library. To know how to install this library in your workspace, you can follow this link. Creating An Empty PDF Doc To cre
3 min read
How to Format the Text in a Word Document using Java?
Apache POI is a project run by the Apache Software Foundation, and previously a sub-project of the Jakarta Project provides pure Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint, and Excel. First do install Apache in order to import modules as per th
3 min read
Adding Pages to a PDF Document using Java
PDDocument class of 'org.apache.pdfbox.pdmodel' package which extends 'java.lang.Object'. is used. Declaration: public class PDDocument extends Object implements Pageable, Closeable Pre-requisite: Constructors PDDocument(): This constructor used to construct a new PDF document with zero pages.PDDocu
4 min read
Create a Table in a PDF Using Java
The creation of a table in a PDF using Java is done by installing the document class. While instantiating this class, pass a PdfDocument object as a parameter to its constructor. Then, to feature a table to the document, instantiate the Table class, and add this object to the document using the add(
1 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
Rotating an Image in a PDF Document Using Java
In this article, we will learn how to Rotating an Image in a PDF document using Java. For Rotating an Image in a PDF, we will use the iText library. These are the steps that should be followed to Rotating an Image in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the
3 min read