Adding and Removing Shapes in Word Documents with Spire.Cloud.Word API

Spire.Cloud provides a web-based Office document editing platform with cloud storage capabilities. The Word API enables programmatic manipulation of Word documents through the ShapesApi interface. This guide demonstrates how to add and delete shapes in Word documents using this API.

Configuration Steps:

1. Register an account at the Spire.Cloud portal and create an application to obtain your App ID and App Key.

2. Upload the target Word document to the cloud storage. Create folders named "input" and "output" to organize input and output files respectively.

3. Configure Maven dependencies in your project:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>cloud</name>
        <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>cloud</groupId>
        <artifactId>spire.cloud.sdk</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.18</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>okhttp</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okio</groupId>
        <artifactId>okio</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>io.gsonfire</groupId>
        <artifactId>gson-fire</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.threeten</groupId>
        <artifactId>threetenbp</artifactId>
        <version>1.3.5</version>
    </dependency>
</dependencies>

4. Implement the shape operations in Java.

Example 1: Adding a Shape

import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ShapesApi;
import spire.cloud.word.sdk.client.model.*;

public class ShapeOperations {
    private static String appId = "YOUR_APP_ID";
    private static String appKey = "YOUR_APP_KEY";
    private static String baseUrl = "https://api.e-iceblue.cn";
    
    private static Configuration config = new Configuration(appId, appKey, baseUrl);
    private static ShapesApi shapesApi = new ShapesApi(config);

    public static void main(String[] args) throws ApiException {
        String filename = "sample.docx";
        String paraPath = "Section/0/Body/0/Paragraph/0";
        int shapeIndex = 1;
        String inputFolder = "input";
        String storage = null;
        String password = null;

        ShapeFormat shapeProps = new ShapeFormat(40f, 40f, ShapeFormat.ShapeTypeEnum.RECTANGLE);
        shapeProps.setHorizontalOrigin(ShapeFormat.HorizontalOriginEnum.PAGE);
        shapeProps.setVerticalOrigin(ShapeFormat.VerticalOriginEnum.PARAGRAPH);
        shapeProps.setVerticalPosition(50f);
        shapeProps.setHorizontalPosition(150f);
        
        Color fillColor = new Color(0, 206, 209);
        shapeProps.setFillColor(fillColor);
        shapeProps.setRotation(45f);
        shapeProps.setStrokeWeight(2f);
        
        Color borderColor = new Color(173, 255, 47);
        shapeProps.setStrokeColor(borderColor);
        shapeProps.setTextWrappingType(ShapeFormat.TextWrappingTypeEnum.BOTH);
        shapeProps.setTextWrappingStyle(ShapeFormat.TextWrappingStyleEnum.INFRONTOFTEXT);
        shapeProps.setZOrder(1);

        String outputPath = "output/shape_added.docx";
        shapesApi.addShape(filename, paraPath, shapeProps, outputPath, inputFolder, storage, shapeIndex, password);
    }
}

Example 2: Deleting a Shape

import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ShapesApi;

public class RemoveShape {
    private static String appId = "YOUR_APP_ID";
    private static String appKey = "YOUR_APP_KEY";
    private static String baseUrl = "https://api.e-iceblue.cn";
    
    private static Configuration config = new Configuration(appId, appKey, baseUrl);
    private static ShapesApi shapesApi = new ShapesApi(config);

    public static void main(String[] args) throws ApiException {
        String filename = "sample.docx";
        String paraPath = "Section/0/Body/0/Paragraph/0";
        Integer shapeIndex = 1;
        String inputFolder = "input";
        String storage = null;
        String password = null;
        String outputPath = "output/shape_removed.docx";

        shapesApi.deleteShape(filename, paraPath, shapeIndex, outputPath, inputFolder, storage, password);
    }
}

Key Parameters:

  • name: Source document filename
  • paragraphPath: XPath to the target paragraph
  • indexInParagraph / index: Shape position within the paragraph
  • folder: Storage folder for input document
  • destFilePath: Output document path
  • shapeFormat: Shape properties including type, dimensions, colors, and positioning

Tags: java Word Spire.Cloud Document Processing API

Posted on Wed, 30 Sep 2026 16:59:53 +0000 by abalfazl