Insert Shapes in Word in Java

This article demonstrates how to add various kinds of shapes and how to group shapes in a Word document using Spire.Doc for Java.

Insert Shapes

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 InsertShapes {

    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
        ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
        rectangle.setFillColor(Color.darkGray);
        rectangle.setStrokeColor(Color.darkGray);
        rectangle.setVerticalPosition(50);

        //insert a triangle
        ShapeObject triangle = para.appendShape((float)(160/Math.sqrt(3)),80, ShapeType.Triangle);
        triangle.setStrokeColor(Color.red);
        triangle.setFillColor(Color.orange);
        triangle.setVerticalPosition(50);
        triangle.setHorizontalPosition(200);

        //insert a ellipse
        ShapeObject circle = para.appendShape(80,80, ShapeType.Ellipse);
        circle.setFillColor(Color.RED);
        circle.setStrokeWeight(15);
        circle.setVerticalPosition(50);
        circle.setHorizontalPosition((float)(270 + 160/Math.sqrt(3)));

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

}

Insert Shapes in Word in Java

Insert Shape Group

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.ShapeGroup;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

public class InsertShapeGroup {

    public static void main(String[] args) {

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

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

        //get page width
        float pageWidth = sec.getPageSetup().getClientWidth();

        //add a shape group, specifying width, height and horizontal position
        ShapeGroup shapegroup = para.appendShapeGroup(200, 150);
        shapegroup.setHorizontalPosition((pageWidth - 200) / 2);

        //calculate the scale ratio
        float X = (shapegroup.getWidth() / 1000.0f);
        float Y = (shapegroup.getHeight() / 1000.0f);

        //create a circle
        ShapeObject circle_1 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_1.setWidth(80 / X);
        circle_1.setHeight(80 / Y);
        circle_1.setFillColor(new Color(176, 196, 222));
        circle_1.setStrokeColor(new Color(176, 196, 222));
        circle_1.setHorizontalPosition(60 / X);//set its horizontal position relative to shape group
        
        //add the circle to shape group
        shapegroup.getChildObjects().add(circle_1);

        //add two more circles to shape group
        ShapeObject circle_2 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_2.setWidth(80 / X);
        circle_2.setHeight(80 / Y);
        circle_2.setFillColor(new Color(0, 128, 128));
        circle_2.setStrokeColor(new Color(0, 128, 128));
        circle_2.setHorizontalPosition(30 / X);
        circle_2.setVerticalPosition(50 / Y);
        shapegroup.getChildObjects().add(circle_2);
        ShapeObject circle_3 = new ShapeObject(doc, ShapeType.Ellipse);
        circle_3.setWidth(80 / X);
        circle_3.setHeight(80 / Y);
        circle_3.setFillColor(new Color(72, 61, 139));
        circle_3.setStrokeColor(new Color(72, 61, 139));
        circle_3.setHorizontalPosition(90 / X);
        circle_3.setVerticalPosition(50 / Y);
        shapegroup.getChildObjects().add(circle_3);
        
        //save the document
        doc.saveToFile("output/InsertShapeGroup.docx", FileFormat.Docx_2010);
    }
}

Insert Shapes in Word in Java