zoukankan      html  css  js  c++  java
  • [Java] Spire.Cloud.Word 在Word 文档中添加、删除形状

    前言:

    Spire.Cloud 在线编辑器是一款基于网页的 Office 文件编辑工具,支持在网页中打开、编辑、打印 Word、Excel、PPT 文件,支持将文档保存到私有云盘。支持 IE、Chrome、FireFox、搜狗、遨游、360 等常见浏览器。Spire.Cloud Web API 能帮助开发人员能在任何时间、任何地点直接调用 SDK 接口对 Word、Excel、PPT、PDF 文档进行操作。Spire.Cloud 支持 .NET、Java、PHP、Python、JavaScript 等多种编程语言,并提供了 1 万次的免费调用次数及 2G 文档内存。

    本文将通过实例阐述,如何通过Spire.Cloud.Word API给开发人员提供的ShapesApi接口,在Word文档中添加和删除形状。

    详细步骤:

    1、通过冰蓝云官网( )注册账号并登陆,在“我的应用”版块创建应用程序,获取你的App ID及App Key。

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

     3、创建Maven应用程序,通过Maven仓库安装Spire.Cloud.SDK jar包及依赖。详细步骤可参考文章

    <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、新建Java class,输入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文档效果图:

    【示例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);
        }
    }

  • 相关阅读:
    常用正则表达式应用
    iOS 通知推送APNS
    IOS生活圈开发练习记录
    [转]定时器
    JavaScript如何在webView跳转到指点的位置
    用imageNamed加载图片产生的问题
    如何再分类中添加属性
    CADisplayLink定时器
    自定义UIBtton类后,在@implementation重写方法的作用
    CAAnimationGroup(组合动画)创建
  • 原文地址:https://www.cnblogs.com/jazz-z/p/13540860.html
Copyright © 2011-2022 走看看