Create and Read Word Document in Java
Published: 2019-04-30 15:49:01

---
title: Create and Read Word Document in Java
published: true
description: word, create, read, java
tags: 
---

##Introduction

Word document is one of the most commonly used file types in our daily work, while create and read Word document are the most popular requirements when working with Word document. In this article, we’ll learn how to create a word document and read the text content in the Word document programmatically in java. 

##The Word Processing Library We Need

[Free Spire.Doc for Java ]( https://www.e-iceblue.com/Introduce/free-doc-for-java.html)

##Create a Word document

In Free Spire.Doc for Java, a **Document** object represents a Word document. The below code example shows how to create a Document object, add paragraphs, set paragraph styles and save the document object to file using Free Spire.Doc for Java.

```java
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

public class CreateWordDocument {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

       //Add 3 paragraphs to the section
        Paragraph para1 = section.addParagraph();
        para1.appendText("Microsoft Word");

        Paragraph para2 = section.addParagraph();
        para2.appendText("Microsoft Word is a word processor developed by Microsoft. It was first released on October 25," +
                "1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several " +
                "other platforms including IBM PCs running DOS (1983), Apple Macintosh running the Classic Mac OS (1985)," +
                "AT&T Unix PC (1985), Atari ST (1988), OS/2 (1989), Microsoft Windows (1989), SCO Unix (1994), "+
                "and Mac OS (formerly OS X; 2001).");

        Paragraph para3 = section.addParagraph();
        para3.appendText("Commercial versions of Word are licensed as a standalone product or as a component of Microsoft Office, " +
                        "Windows RT or the discontinued Microsoft Works suite. Microsoft Word Viewer and Office Online are freeware editions "+
                        "of Word with limited features.");

        //Set title style for paragraph 1
        ParagraphStyle style1 = new ParagraphStyle(document);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(Color.BLUE);
        style1.getCharacterFormat().setFontName("Arial");
        style1.getCharacterFormat().setFontSize(12f);
        document.getStyles().add(style1);
        para1.applyStyle("titleStyle");

        //Set style for paragraph 2 and 3
        ParagraphStyle style2 = new ParagraphStyle(document);
        style2.setName("paraStyle");
        style2.getCharacterFormat().setFontName("Arial");
        style2.getCharacterFormat().setFontSize(10f);
        document.getStyles().add(style2);
        para2.applyStyle("paraStyle");
        para3.applyStyle("paraStyle");

        //Horizontally align paragraph 1 to center
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //Set indent for paragraph 2 and 3
        para2.getFormat().setFirstLineIndent(15f);
        para3.getFormat().setFirstLineIndent(15f);

        //Set spaces after paragraph 1 and 2
        para1.getFormat().setAfterSpacing(15f);
        para2.getFormat().setAfterSpacing(10f);

        //Save the document
        document.saveToFile("CreateWordDocument.docx", FileFormat.Docx);
    }
}
```
![Create Word](https://thepracticaldev.s3.amazonaws.com/i/edlva46lsnymb7jc8bbc.png)

 
##Read Text Content in a Word document

**Document** class provides a **getText()** method which allows us to easily get the text content of a Word document.

```java
import com.spire.doc.*;

public class TextReader {
    public static void main(String[] args){

        //Load the Word document
        Document doc = new Document("CreateWordDocument.docx");

        //Get text
        System.out.println(doc.getText());
    }
}
```
![Read Word Document](https://thepracticaldev.s3.amazonaws.com/i/j021apwanm5pbtn6dvhr.png)
 
Besides text, Free Spire.Doc for Java also supports to read a variety of other elements in a Word document, such as paragraph, image, header, footer, list and table. You can have a try by yourself.