Spire.Cloud.Word 在 Word 文档中添加、删除形状

 

Spire.Cloud.Word 为开发人员提供了ShapesApi接口用于操作Word文档中的形状。本文将介绍如何使用该接口在Word文档中添加和删除形状。

详细步骤如下:

步骤1、创建一个Maven程序,通过Maven仓库安装Spire.Cloud.SDK jar包及依赖,详细步骤可参考这篇文章

步骤2、通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获取你的App ID及App Key。

步骤3、上传Word文档至冰蓝云官网的“文档管理”版块。为了便于文档管理,您也可以先创建文件夹“input”和“output”,然后将需要编辑的Word文档上传至input文件夹下,output文件夹用于存放生成的文档。

Spire.Cloud.Word 在 Word 文档中添加、删除形状

步骤4、在Java程序中编写代码来添加、删除Word形状。

示例1、添加形状

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.e-iceblue.cn";
    //配置App ID和App Key
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    //创建ShapesApi实例
    static ShapesApi shapesApi = new ShapesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        //示例文档名称
        String name = "addShape.docx";
        //段落路径
        String paragraphPath = "Section/0/Body/0/Paragraph/0";
        //需要添加形状的段落
        int indexInParagraph = 1;
        //存放示例文档的文件夹,没有则为null
        String folder = "input";
        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;
        //示例文档密码,没有则为null
        String password = null;

        //创建形状,并设置其形状类型、位置、填充颜色、旋转角度、文字环绕方式等
        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;

        //指定生成文档的存放路径
        String destFilePath = "output/addShape_output.docx";

        //调用addShape方法添加形状到Word文档
        shapesApi.addShape(name, paragraphPath, shapeFormat, destFilePath,folder, storage, indexInParagraph, password);
    }
}

使用Spire.Cloud在线编辑查看添加形状后的Word文档效果图:

Spire.Cloud.Word 在 Word 文档中添加、删除形状

示例2、删除形状

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


public class deleteShape {
    static String appId = "APP ID";
    static String appKey = "APP Key";
    static String baseUrl = "https://api.e-iceblue.cn";
    //配置App ID和App Key
    static Configuration wordConfiguration = new Configuration(appId, appKey, baseUrl);
    //创建ShapesApi实例
    static ShapesApi shapesApi = new ShapesApi(wordConfiguration);

    public static void main(String[] args) throws ApiException {
        //示例文档名称
        String name = "getShape.docx";
        //段落路径
        String paragraphPath = "Section/0/Body/0/Paragraph/0";
        //指定需要删除形状的索引
        Integer index = 1;
        //存放示例文档的文件夹,没有则为null
        String folder = "input";
        //使用冰蓝云配置的2G空间存贮文档,可设置为null
        String storage = null;
        //示例文档密码,没有则为null
        String password = null;
        //指定生成文档的存放路径
        String destFilePath = "output/deleteShape.docx";

        //调用deleteShape方法删除Word文档中的形状
        shapesApi.deleteShape(name, paragraphPath, index, destFilePath, folder, storage, password);
    }
}

Spire.Cloud.Word 在 Word 文档中添加、删除形状