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 manufacturing companies generate invoices based on the data stored in the databases. In such cases, document automation makes it possible to save time, efforts and resources required in the manual document creation process. This article aims to target the document automation process and lets you know how to create Word documents programmatically in Java.
- Java API 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 API to Create Word Documents
In this article, we’ll use Aspose.Words for Java which is a feature-rich API 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>20.2</version>
<classifier>jdk17</classifier>
</dependency>
In the following sections, you’ll learn how to create a Word document containing different elements such as text, paragraph, table, list, images, etc. programmatically using Java.
Create a Word Document using 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:
- 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, and 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.
The following code sample shows how to create a Word document containing text in Java.
Output
Create a Word Document having Table using Java
Tables in the 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 using 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 create a table in a Word document.
Output
Create a List in Word Document using Java
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
Insert Image in a Word Document using Java
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 to the Word document using Java.
Output
Conclusion
In this article, you have learned how to create rich Word documents programmatically using Java. Furthermore, the code samples have shown how to add text, tables, images, and lists to Word documents. You can read more about how to insert various elements into a Word document using Aspose.Words for Java.
Try Aspose.Words for Java for Free
You can get a free temporary license to try and use Aspose.Words for Java without trial limitations.