Spire.Cloud.Word 提供了 ParagraphsApi 接口用于操作 Word 中的段落,包括添加、修改、删除段落,以及对获取段落中的子对象、设置段落格式等。本文将介绍如何添加、修改和删除段落。
步骤一:创建 .NET 应用程序,通过 NuGet 搜索安装 Spire.Cloud.Sdk,详细步骤可参考这篇文章。
步骤二:通过冰蓝云官网(https://cloud.e-iceblue.cn/)注册账号并登陆,在“我的应用”版块创建应用程序,获得 App ID 及 App Key。

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

步骤四:在 .NET 程序中编写代码操作 input 文件夹下的文档。
示例 1、添加段落
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
using System;
namespace CloudAddParagraph
{
    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);
            //初始化ParagraphsApi对象
            ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
            //现有文档名称
            string fileName = "示例文档.docx";
            //选择需要插入段落节、正文
            string nodePath = "Section/0/Body/0";
            //设置生成文档的路径及名称
            string destFilePath = "output/AddParagraph.docx";
            //存放现有文档的文件夹,如果没有文件夹则为null
            string folder = "input";
            //使用冰蓝云默认空间存贮文档,可设置为null
            string storage = null;
            //插入段落的位置
            int indexOfParagraph = 2;
            //文档的打开密码
            string password = null;
            //添加段落的文字
            string text = "这是新加的段落";
            //通过AddParagraph方法添加段落
            paragraphsApi.AddParagraph(fileName, nodePath, destFilePath, folder, storage, indexOfParagraph, password, text);         
        }
    }
}

示例 2、修改段落
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace UpdateParagraph
{
    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);
            //初始化ParagraphsApi对象
            ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
            //现有文档名称
            string fileName = "示例文档.docx";
            //选择节、正文
            string nodePath = "Section/0/Body/0";
            //要修改段落的索引
            int index = 2;
            string text = "高质量的文档转换功能";
            //存放现有文档的文件夹,如果没有文件夹则为null
            string folder = "input";
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
            //文档的打开密码
            string password = null;
            //设置生成文档的路径及名称
            string destFilePath = "output/修改段落.docx";
            //通过UpdateParagraphText方法修改指定段落的文本
            paragraphsApi.UpdateParagraphText(fileName, nodePath, index, text, destFilePath, folder, storage, password);
        }
    }
}

示例 3、删除段落
using System;
using Spire.Cloud.Word.Sdk.Api;
using Spire.Cloud.Word.Sdk.Client;
namespace DeleteParagraph
{
    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);
            //初始化ParagraphsApi对象
            ParagraphsApi paragraphsApi = new ParagraphsApi(configuration);
            //现有文档名称
            string fileName = "示例文档.docx";
            //选择节、正文
            string nodePath = "Section/0/Body/0";
            //要删除段落的索引
            int index = 2;
            //存放现有文档的文件夹,如果没有文件夹则为null
            string folder = "input";
            //使用冰蓝云配置的2G空间存贮文档,可设置为null
            string storage = null;
            //文档的打开密码
            string password = null;
            //设置生成文档的路径及名称
            string destFilePath = "output/删除段落.docx";
            //通过DeleteParagraph方法删除指定段落
            paragraphsApi.DeleteParagraph(fileName, nodePath, index, destFilePath, folder, storage, password);
        }
    }
}

											