Spire.Cloud.Word 添加、删除 Word 形状

 

Spire.Cloud.Word提供了ShapesApi接口用于操作Word中的形状,本文将介绍如何添加和删除形状。

步骤一:创建.NET应用程序,通过NuGet搜索安装Spire.Cloud.Sdk,详细步骤可参考这篇文章

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

Spire.Cloud.Word 添加、删除 Word 形状

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

Spire.Cloud.Word 添加、删除 Word 形状

步骤四:在.NET程序中编写代码操作input文件夹的下的文档。


示例1、添加形状

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

namespace InsertShape
{
    class Program
    {
        static String appId = "App ID";
        static String appKey = "App Key";

        static void Main(string[] args)
        {     
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

            //初始化ShapesApi对象
            ShapesApi shapesApi = new ShapesApi(configuration);

            //现有文档名称
            string fileName = "示例文档.docx";

            //段落的路径
            string paragraphPath = "sections/0/paragraphs/0";

            //创建形状
            ShapeFormat shapeFormat = new ShapeFormat(150f, 50, ShapeFormat.ShapeTypeEnum.Rectangle)
            {
                HorizontalOrigin = ShapeFormat.HorizontalOriginEnum.Margin,
                VerticalOrigin = ShapeFormat.VerticalOriginEnum.Margin,
                VerticalPosition = 0,
                HorizontalPosition = 0,
                FillColor = new Color(200, 0, 0),
                StrokeWeight = 2,
                StrokeColor = new Color(125, 125, 125),
                TextWrappingStyle = ShapeFormat.TextWrappingStyleEnum.Through,
            };

            //存放现有文档的文件夹,如果没有文件夹则为null
            string folder = "input";

            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;

            //形状在段落中的位置
            int indexInPara = 1;

            //文档的打开密码
            string password = null;

            //设置生成文档的路径及名称
            string destFilePath = "output/添加形状.docx";

            //将形状添加到指定位置
            shapesApi.AddShape(fileName, paragraphPath, shapeFormat, folder, storage, indexInPara, password, destFilePath);        
        }
    }
}

Spire.Cloud.Word 添加、删除 Word 形状

示例2、删除形状

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)
        {
            //配置App ID和App Key
            Configuration configuration = new Configuration(appId, appKey);

            //初始化ShapesApi对象
            ShapesApi shapesApi = new ShapesApi(configuration);

            //现有文档名称
            string fileName = "示例文档.docx";

            //段落的路径
            string paragraphPath = "sections/0/paragraphs/0";

            //要删除形状的索引
            int shapeIndex = 0;

            //存放现有文档的文件夹,如果没有文件夹则为null
            string folder = "input";

            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;

            //文档的打开密码
            string password = null;

            //设置生成文档的路径及名称
            string destFilePath = "output/删除形状.docx";

            //根据索引删除指定形状
            shapesApi.DeleteShape(fileName, paragraphPath, shapeIndex, folder, storage, password, destFilePath);
        }
    }
}