Open In App

How to Create a Blank Word Document using Java?

Last Updated : 08 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. Creating a blank document using file methods.
  2. Specifying the path or directory where the blank document is to be created.
  3. Writing to the document in the file stream.
  4. Writings contents to the document.
  5. 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




// Java Program to Create 
// a Blank Word file
  
// Importing File libraries
import java.io.File;
import java.io.FileOutputStream;
  
// Importing Apache libraries
import org.apache.poi.xwpf.usermodel.XWPFDocument;
  
public class GFG {
  
    // Main driver method
    public static void main(String[] args) throws Exception
    {
  
        // Create a blank document
        XWPFDocument xwpfdocument = new XWPFDocument();
  
        // Create file by specifying the path
        File file = new File("C:/blankdocument.docx");
  
        // Writing document in file stream
        FileOutputStream ostream
            = new FileOutputStream(file);
  
        // Write contents to the document
        xwpfdocument.write(ostream);
  
        // Close the file connection
        ostream.close();
    }
}


Output: The code creates a blank document file in the local directory as specified by the programmer in the code.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads