Java - Insert or Read WordArt in Word Documents
Tags: java, word, wordart
Published: 2022-09-01 09:53:34

WordArt is a set of text styles that allows you to add design elements such as fills, outlines and shadows to text. Inserting WordArt is a great way to make your text standout and eye-catching. When creating a document in Microsoft Word, you may sometimes need to insert WordArt. This article is going to explain how to **insert or read WordArt in Word documents in Java** using [Free Spire.Doc for Java](https://www.e-iceblue.com/Introduce/free-doc-for-java.html) library.

## Add Dependencies
Before coding, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that.

**Method 1:** If you are using maven, you can easily import the JAR file of Free Spire.Doc for Java into your application by adding the following code to your project’s pom.xml file.
```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>
```
**Method 2:** If you are not using maven, you can download Free Spire.Doc for Java from [this link](https://www.e-iceblue.com/Download/doc-for-java-free.html), extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

## Insert WordArt into Word using Java
The following are the main steps to insert a WordArt into a Word document:

- Create an instance of **Document** class.
- Load a Word document using **Document.loadFromFile()** method.
- Get the desired section of the document using **Document.getSections().get(sectionIndex)** method.
- Add a paragraph to the section using **Section.addParagraph()** method.
- Add a shape of specified size and type to the paragraph using **Paragraph.appendShape()** method.
- Set the position for the shape.
- Insert a WordArt with specified text to the shape using **ShapeObject.getWordArt().setText()** method.
- Set fill color and border color for the WordArt.
- Save the result document using **Document.saveToFile()** method.
```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class InsertWordArtInWord {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("input.docx");

        //Get the first section
        Section section = doc.getSections().get(0);
        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();

        //Add a shape to the paragraph
        ShapeObject shape = paragraph.appendShape(250, 70, ShapeType.Text_Wave_3);

        //Set the position of the shape
        shape.setVerticalPosition(20);
        shape.setHorizontalPosition(80);

        //Set the text of WordArt
        shape.getWordArt().setText("Happy Birthday");

        //Set the fill color
        shape.setFillColor(Color.orange);

        //Set the border color of the text.
        shape.setStrokeColor(Color.YELLOW);

        //Save the result document
        doc.saveToFile("InsertWordArt.docx", FileFormat.Docx);
    }
}
```

![Insert WordArt into Word using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqbs69vnycddi2tnunn5.png) 

## Read WordArt in Word using Java
The following are the main steps to read WordArt in a Word document:

- Create an instance of **Document** class.
- Load a Word document using **Document.loadFromFile()** method.
- Loop through all the sections in the document.
- Loop through all the paragraphs in each section.
- Loop through all the child objects in each paragraph.
- Detect if the child object is a **ShapeObject**.
- Get the WordArt text in the shape object using **ShapeObject.getWordArt().getText()** method.
- If the text is not null, print it to the console.
```java
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.ShapeObject;

public class ReadWordArt {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a word document
        doc.loadFromFile("InsertWordArt.docx");

        //Loop through all the sections in the document
        for (Section section :(Iterable<? extends Section>) doc.getSections()) {
            //Loop through all the paragraphs in each section
            for (Paragraph paragraph : (Iterable<? extends Paragraph>) section.getBody().getParagraphs()) {
                //Loop through all the child objects in each paragraph
                for (DocumentObject documentObject : (Iterable<? extends DocumentObject>) paragraph.getChildObjects()) {
                    //Detect if the child object is a shape
                    if (documentObject instanceof ShapeObject) {
                        ShapeObject shapeObject = (ShapeObject) documentObject;
                        //Detect if the shape is a WordArt
                        String text = shapeObject.getWordArt().getText();
                        if (text != "") {
                            //Read the WordArt text
                            System.out.println("WordArt Text：" + text);
                        }
                    }
                }
            }
        }
    }
}
```

![Read WordArt in Word using Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5siqkdblhd6o9tp2n5pi.png)