Java: Insert WordArt in Word

Friday, 24 June 2022 06:51 Written by  support iceblue
Rate this item
(0 votes)

In MS Word, WordArt is used to insert text with special effects such as shadows, outlines, colors, gradients, and 3D effects. It can be helpful when creating stylish headlines or highlighting specific content. In this article, you will learn how to programmatically insert WordArt in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>10.9.0</version>
    </dependency>
</dependencies>
    

Insert WordArt in Word

The ShapeType enumeration provided by Spire.Doc for Java defines a variety of WordArt shape types whose names begin with "Text". In order to create a WordArt in Word, you need to initialize an instance of ShapeObject and specify the WordArt type and text content. The detailed steps are as follows:

  • Create a Document instance.
  • Add a section to the document using Document.addSection() method, and then add a paragraph to the section using Section.addParagraph() method.
  • Append a shape to the paragraph and specify the shape size and type using Paragraph.appendShape(float width, float height, ShapeType shapeType) method.
  • Set the position of the shape using ShapeObject.setVerticalPosition() and ShapeObject.setHorizontalPosition() methods.
  • Set the text of WordArt using ShapeObject.getWordArt().setText() method.
  • Set the fill color and stroke color of WordArt using ShapeObject.setFillColor() and ShapeObject.setStrokeColor() methods.
  • Save the document to another file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;


public class WordArt{
    public static void main(String[] args) throws Exception {
        //Create a Document instance
        Document doc = new Document();

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

        //Add a paragraph.
        Paragraph paragraph = section.addParagraph();

        //Add a shape to the paragraph and specify the shape size and type
        ShapeObject shape = paragraph.appendShape(400, 150, ShapeType.Text_Deflate_Bottom);

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

        //set the text of WordArt
        shape.getWordArt().setText("Create WordArt in Word");

        // Set the fill color and stroke color of WordArt
        shape.setFillColor(Color.CYAN);
        shape.setStrokeColor(Color.BLUE);

        //save the document to file.
        doc.saveToFile("WordArt.docx", FileFormat.Docx_2013);
    }
}

Java: Insert WordArt in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Monday, 26 September 2022 06:54