Java Program to Create a Word Document

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:
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:
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.
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:
 
 
rs1686740
simmytarika5
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Different ways of Reading a text file in Java
Constructors in Java
Strings in Java
HashMap containsKey() Method in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
Traverse Through a HashMap in Java
Extends vs Implements in Java