In recent years, the dynamic generation of Word documents has become a popular feature for composing reports, quotes, invoices, and other types of documents. Various libraries are available that allow you to generate MS Word documents programmatically in Java. However, some of them need MS Office and others require you to write complex code. Considering such issues, this article aims to provide you with the best yet simplest way to create MS Word documents in Java without requiring MS Office. Also, you will learn how to automate MS Word features with minimum lines of simple code.
- Java Library to Create Word Documents
- Create a Word Document using Java
- Create a Word Document having Table
- Insert a List in Word Document
- Create Word Document having Image
Java Library to Create Word Documents
In this article, we’ll use Aspose.Words for Java, which is a feature-rich library to create, edit, or convert Word documents in Java-based applications. You can download the API’s JAR or install it using the following Maven configurations:
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>22.11</version>
<classifier>jdk17</classifier>
</dependency>
In the following sections, you’ll learn how to create a Word document containing different elements such as text, paragraphs, tables, lists, images, etc. programmatically using Java.
How to Create a Word Document in Java
Most of the time, a considerable portion of the content in a Word document is based on the text. Therefore, we’ll start our journey by creating a Word document with a heading and a paragraph.
The following are the steps to create a Word document from scratch:
- First, create an instance of Document class.
- Create a DocumentBuilder object and initialize it with the Document object.
- Use the Font class and set font size, family, etc.
- Set paragraph’s properties using ParagraphFormat class.
- Write text to the document using DocumentBuilder.write() method.
- Finally, call Document.save() method to create the document.
Java Program to Create a Word Document
The following code sample shows how to create a Word document containing text in Java.
Output
Generate Table in a Word Document in Java
Tables in Word documents are used to organize the content in the form of rows and columns. In this section, we’ll create a simple table containing two rows and two columns. Creating a table includes four basic operations:
- Starting the table
- Inserting a cell
- Ending the row
- Ending the table
The following are the step to create a table in Word document in Java:
- First, create the objects of Document and DocumentBuilder class.
- Create a table using Table class.
- Insert a cell using DocumentBuilder.insertCell() method.
- Set table’s properties as per your requirements.
- Add text to the cell using DocumentBuilder.write() method.
- End the row and table using DocumentBuilder.endRow() and DocumentBuilder.endTable() methods respectively.
- Finally, save the document.
The following sample shows how to generate a Word document and add a table to it.
Output
Java Word Automation - Insert a List Document
The following are the steps to add a list to the Word document.
- First, create an object of Document class.
- Add your desired type of list to the document using Document.getLists().add() method.
- Get the list from the document into a List object.
- Populate the list using the DocumentBuilder object.
- Finally, save the document.
The following code sample shows how to create a list in Word document using Java.
Output
Java Generate a Word Document and Insert Image
The insertion of an image into a Word document is as simple as pie. The following are some simple steps to perform this operation:
- First, create an object of Document class.
- Create an object of DocumentBuilder class and initialize it with the Document object.
- Insert image using DocumentBuilder.insertImage() method.
- Finally, save the document.
The following code sample shows how to insert an image into the Word document in Java.
Output
Create MS Word Documents with a Free License
You can get a free temporary license and create MS Word documents from scratch without evaluation limitations.
Java Word Automation Library - Read More
You can read more about how to insert various elements into a Word document using Aspose.Words for Java.
Conclusion
In this article, you have learned how to create rich Word documents in Java. Furthermore, the code samples have shown how to add text, tables, images, and lists to Word documents. Thus, you can easily implement MS Word automation features in your Java applications.