Spire.Cloud.Word provides the ShapesApi interface for developers to manipulate shapes in Word. This tutorial will show you how to add and delete a shape in Word using Java codes.
Add a shape to Word
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ShapesApi;
import spire.cloud.word.sdk.client.model.*;
public class AddShape {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//create a configuration object based on your App ID and App Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a ShapesApi instance
static ShapesApi shapesApi = new ShapesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a sample document
String name = "Sample.docx";
//Specify the paragraph path
String paragraphPath = "Section/0/Body/0/Paragraph/0";
//Specify the paragraph to add a shape
int indexInParagraph = 1;
//Specify the folder storing the sample, and it's null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, and it's null by default
String storage = null;
//Specify the password of the sample, and it's null if nothing
String password = null;
//Create a shape, and set its style, location, filling color, rotation angle and so on
ShapeFormat shapeFormat = new ShapeFormat(40f, 40f, ShapeFormat.ShapeTypeEnum.RECTANGLE);
shapeFormat.setHorizontalOrigin(ShapeFormat.HorizontalOriginEnum.PAGE);
shapeFormat.setVerticalOrigin(ShapeFormat.VerticalOriginEnum.PARAGRAPH);
shapeFormat.setVerticalPosition(50f);
shapeFormat.setHorizontalPosition(150f);
Color color = new Color(0, 206, 209);
shapeFormat.setFillColor(color);
shapeFormat.setRotation(45f);
shapeFormat.setStrokeWeight(2f);
Color color_2 = new Color(173, 255, 47);
shapeFormat.setStrokeColor(color_2);
shapeFormat.setTextWrappingType(ShapeFormat.TextWrappingTypeEnum.BOTH);
shapeFormat.setTextWrappingStyle(ShapeFormat.TextWrappingStyleEnum.INFRONTOFTEXT);
shapeFormat.setZOrder(1);
ShapeFormat shapeProperties = shapeFormat;
//Specify the storage path of the resulting document
String destFilePath = "addShape.docx";
//Call the addShape method to add a shape to Word
shapesApi.addShape(name, paragraphPath, shapeFormat, destFilePath,folder, storage, indexInParagraph, password);
}
}
Output
Delete a shape in Word
import spire.cloud.word.sdk.client.*;
import spire.cloud.word.sdk.client.api.ShapesApi;
public class DeleteShapes {
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.cloudxdocs.com";
//Create a Configuration object based on your App ID and App Key
static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
//Create a ShapesApi instance
static ShapesApi shapesApi = new ShapesApi(wordConfiguration);
public static void main(String[] args) throws ApiException {
//Load a sample document
String name = "addShape.docx";
//Specify the paragraph path
String paragraphPath = "Section/0/Body/0/Paragraph/0";
//Specify the specified shape to be deleted by index
Integer index = 1;
//Specify the folder storing the sample, and it's null if nothing
String folder = "input";
//Use the 2G storage provided by E-iceblue, and it's null by default
String storage = null;
//Specify the password of the sample, and it's null if nothing
String password = null;
//Specify the storage path of the resulting document
String destFilePath = "deleteShape.docx";
//Call the deleteShape method to delete a shape in Word
shapesApi.deleteShape(name, paragraphPath, index, destFilePath, folder, storage, password);
}
}