MS Word Automation – Create, Edit, or Convert Word Documents using Java

Microsoft Word Automation — Create, Edit, or Convert Word Documents using Java

Automation of Word enables you to create new Word documents, edit or modify the existing ones, or convert them into other formats without using Microsoft Office. All the actions that we can perform through the user interface of MS Word can also be performed programmatically by using Automation. In this article, we will learn how to automate MS Word to create, edit, or convert Word documents using Java.

The following topics shall be covered in this article:

Java Word Automation API to Create, Edit, or Convert Word Documents

For automating Word, we will be using Aspose.Words for Java API. It is a complete and feature-rich Word automation solution to create, edit, or analyze Word documents programmatically. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>21.11</version>
    <type>pom</type>
</dependency>

Create Word Documents using Java

We can create Word documents programmatically by following the steps given below:

  • Firstly, create an instance of the Document class. This class represents a Word document.
  • Next, create an instance of the DocumentBuilder class with the Document object as argument. This class provides methods to insert text, images and other content, specify font, paragraph, and section formatting.
  • Then, insert/write elements to add some text, paragraphs, table, or image using the DocumentBuilder object.
  • Finally, call the Document.save() method with the output file path to save the created file.

The following code sample shows how to create a Word document (DOCX) using Java.

Create Word Documents using Java
Create Word Documents using Java.

Edit or Modify Word Documents using Java

In the previous section, we have created a Word document. Now, let’s edit it and change the content of the document. We can edit Word documents programmatically by following the steps given below:

  • Firstly, load a Word document using the Document class with input file path as argument.
  • Next, access the specific section by its index.
  • Then, access the first paragraph content as an object of the Run class. The Run class represents a run of characters with the same font formatting. All the text of the document is stored in runs of text.
  • After that, set the text to update for the accessed paragraph.
  • Finally, call the Document.save() method with the output file path to save the updated file.

The following code sample shows how to edit a Word document (DOCX) using Java.

Edit or Modify Word Documents using Java
Edit or Modify Word Documents using Java.

Find and Replace Text in Word Documents using Java

We can also update the content of Word documents by using the find and replace mechanism of the API. We can do this programmatically by following the steps given below:

  • Firstly, load a Word document using the Document class with input file path as argument.
  • Next, create an instance of the FindReplaceOptions class.
  • Then, call the replace() method with the search string, the replace string and the FindReplaceOptions object as arguments. This method shall replace every occurrence of the search string with the replace string.
  • Finally, call the Document.save() method with the output file path to save the updated file.

The following code sample shows how to find and replace specific text in a Word document (DOCX) using Java.

Find and Replace Text in Word Documents using Java
Find and Replace Text in Word Documents using Java

Convert Word Documents using Java

We can convert Word documents to other formats such as PDF, XPS, EPUB, HTML, JPG, PNG, etc. programmatically by following the steps given below:

  • Firstly, load a Word document using the Document class with input file path as argument.
  • Next, create an instance of the PdfSaveOptions class with the Document object as argument. This class provides additional options when saving a document into the PDF.
  • Then, specify the PDF standards compliance level for the output document by setting the PdfSaveOptions.Compliance to PdfCompliance.PDF_17.
  • Finally, call the Document.save() method with the output file path and PdfSaveOptions object as argument to save the PDF file.

The following code sample shows how to convert a Word document (DOCX) to a PDF using Java.

Convert Word Documents to PDF.
Convert Word Documents using Java

Parse Word Documents using Java

We can parse Word documents and extract the content as plain text programmatically by following the steps given below:

  • Load a Word document using the Document class with input file path as argument.
  • Call the Document.save() method to save the Word document as a text file. This method takes the path of the output file as an argument.

The following code sample shows how to parse a Word document (DOCX) using Java.

Extract text from Word Documents.
Parse Word Documents using Java.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to automate Word to create, edit, or convert Word documents using Java. We have also seen how to find and replace text in Word documents programmatically. Besides, you can learn more about Aspose.Words for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also