Java adding, formatting and rotating shapes in word document
Tags: shapes, word, java
Published: 2019-10-29 03:53:10

---
title: Java adding, formatting and rotating shapes in word document
published: true
description: Working with Word shapes in Java
tags: shapes, word, java
---

Shapes are drawing objects that include arrows, lines, curves, circles, rectangles, ellipse etc. It makes the word document vivid and beautiful. With the help of Spire.Doc, we can create and manipulate the pre-defined shape in DOCX format documents easily.

We could set the formatting to the shapes to make them more beautiful, such as line color, fill color, set the position of the shape etc.  Here we will how to create rectangle and ellipse shapes to word document and set format options for them.

```java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class AddingShapes {
    public static void main(String[] args) throws Exception {

                //create a Word document.
                Document doc = new Document();

                //add a section and a paragraph
                Section sec = doc.addSection();
                Paragraph para = sec.addParagraph();

                //insert a rectangle and format shape
                ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
                rectangle.setFillColor(Color.cyan);
                rectangle.setStrokeColor(Color.CYAN);
                rectangle.setVerticalPosition(50);

                //insert a ellipse
                ShapeObject circle = para.appendShape(200,80, ShapeType.Ellipse);
                circle.setFillColor(Color.blue);
                circle.setStrokeWeight(5);
                circle.setStrokeColor(Color.GRAY);
                circle.setVerticalPosition(50);
                circle.setHorizontalPosition((float)(200 + 160/Math.sqrt(3)));

                //save to file
                doc.saveToFile("output/InsertShapes.docx", FileFormat.Docx);
            }

        }
```
Output:
![Adding shapes](https://thepracticaldev.s3.amazonaws.com/i/fptf4vfyhdn0ihwgs2ob.png)
 
Rotate shapes: We can rotate the shape and also apply flipping (horizontal and vertical) to it. The following code example explains how to rotate the shapes in Word document in Java applications.

```java
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;

public class RotateShapes {
    public static void main(String[] args) throws Exception {

               //Load the Sample Word document.
                Document doc = new Document();
                doc.loadFromFile("InsertShapes.docx");

               //Get the first section
                Section sec = doc.getSections().get(0);

                //Traverse every paragraphs and get the shapes and rotate them
                 for ( Paragraph para: (Iterable<Paragraph>) sec.getParagraphs()) {
                   for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects())   {

                      if (obj instanceof ShapeObject) {
                       ((ShapeObject) obj).setRotation(20);
                  }
             }
        }
                //save to file
                doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);

    }
}

```
Output:
![Rotating shapes](https://thepracticaldev.s3.amazonaws.com/i/mhlsa00v2te1ds585f1s.png)
 
With Spire.Doc for Java, we can also group the shapes and delete the shapes from the word document. Wish it helps.