温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何调用shapesApi接口操作Word形状

发布时间:2020-06-04 11:08:37 来源:亿速云 阅读:192 作者:Leah 栏目:编程语言

这篇文章主要为大家详细介绍了调用shapesApi接口操作Word形状的方法,图文详解容易学习,非常适合初学者入门,感兴趣的小伙伴们可以参考一下。

步骤一:dll文件获取及导入。通过官网本地下载SDK文件包。(须在e-iceblue中国官网在线编辑板块中注册账号并登录)

如何调用shapesApi接口操作Word形状

下载后,解压文件,将Spire.Cloud.Word.Sdk.dll文件及其他三个dll添加引用至VS程序;或者在程序中通过Nuget搜索下载,直接导入所有dll。dll引用结果如下图所示:
如何调用shapesApi接口操作Word形状
步骤二:App ID及Key获取。在“我的应用”板块中创建应用以获得App ID及App Key。
如何调用shapesApi接口操作Word形状
步骤三:源文档上传。在“文档管理”板块,上传源文档。这里可以建文件夹,将文档存放在文件夹下。不建文件夹时,源文档及结果文档直接保存在根目录。本文示例中,建了两个文件夹,分别用于存放源文档及结果文档。(云平台提供免费1 万次调用次数和 2G 文档内存)
如何调用shapesApi接口操作Word形状


C# 代码示例
1. 添加形状到Word

using System;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Model;

namespace AddShape
{
    class Program
    {
        static string appId = "App ID";
        static string appKey = "App Key";
        static void Main(string[] args)
        {
            //配置AppID和AppKey
            Configuration wordConfiguration = new Configuration(appId, appKey);

            //实例化ShapesApi类
            ShapesApi shapesApi = new ShapesApi(wordConfiguration);

            string name = "test.docx";//源文档
            string paragraphPath = "sections/0/paragraphs/0";//段落路径
            int indexInParagraph = 1;//添加形状的段落
            string folder = "input";//源文档所在文件夹
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
            string password = null;//源文档密码

            //设置形状属性(包括形状类型、位置、填充颜色、旋转方向、边框宽度/颜色、文本环绕类型/方式
            ShapeFormat shapeProperties = new ShapeFormat(50, 50, ShapeFormat.ShapeTypeEnum.Star)
            {
                HorizontalOrigin = ShapeFormat.HorizontalOriginEnum.Page,
                VerticalOrigin = ShapeFormat.VerticalOriginEnum.Page,
                VerticalPosition = 40,
                HorizontalPosition = 230,
                FillColor = new Color(255, 69, 0),
                Rotation = 45,
                StrokeWeight = 2,
                StrokeColor = new Color(255, 255, 0),
                TextWrappingType = ShapeFormat.TextWrappingTypeEnum.Both,
                TextWrappingStyle = ShapeFormat.TextWrappingStyleEnum.InFrontOfText,
                ZOrder = 1
            };
            string destFilePath = "output/AddShape.docx";//结果文档路径

            //调用方法添加形状
            shapesApi.AddShape(name, paragraphPath, shapeProperties, folder, storage, indexInParagraph, password, destFilePath);
        }
    }
}

形状添加效果:
如何调用shapesApi接口操作Word形状

2. 删除Word中的形状

using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;

namespace DeleteShape
{
    class Program
    {
        static string appId = "App ID";
        static string appKey = "App Key";
        static void Main(string[] args)
        {
            //配置AppID和AppKey
            Configuration wordConfiguration = new Configuration(appId, appKey);

            //实例化ShapesApi类
            ShapesApi shapesApi = new ShapesApi(wordConfiguration);

            string name = "AddShape.docx";//源文档
            string paragraphPath = "sections/0/paragraphs/0";//段落路径
            int index = 0;//要删除形状的索引
            string folder = "output";//源文档所在文件夹
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
            string password = null;//源文档密码

            string destFilePath = "output/DeleteShape.docx";//结果文档路径

            //调用方法删除形状
            shapesApi.DeleteShape(name, paragraphPath, index, folder, storage, password, destFilePath);
        }
    }
}

形状删除效果:
如何调用shapesApi接口操作Word形状
3. 读取Word形状属性

using System;
using Spire.Cloud.Word.Sdk.Client;
using Spire.Cloud.Word.Sdk.Api;

namespace GetShapeProperties
{
    class Program
    {
        static string appId = "App ID";
        static string appKey = "App Key";
        static void Main(string[] args)
        {
            //配置AppID和AppKey
            Configuration wordConfiguration = new Configuration(appId, appKey);

            //实例化ShapesApi类
            ShapesApi shapesApi = new ShapesApi(wordConfiguration);

            string name = "AddShape.docx";//源文档
            string paragraphPath = "sections/0/paragraphs/0"; 
            int index = 0;//读取的形状索引
            string folder = "output";//源文档所在文件夹
            string storage = null;//使用冰蓝云配置的2G空间存贮文档,可设置为null
            string password = null;//源文档密码

            //读取属性
            System.Console.WriteLine(shapesApi.GetShapeProperties(name, paragraphPath, index, folder, storage, password));
            System.Console.ReadLine();

        }
    }
}

属性读取结果:
如何调用shapesApi接口操作Word形状

以上就是调用shapesApi接口操作Word形状方法的详细介绍,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注亿速云行业资讯!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI