C#添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)

本文介紹調(diào)用Spire.Cloud.Word.SDK提供的接口shapesApi來操作Word形狀,包括添加形狀AddShape(),添加形狀時,可設置形狀類型、顏色、大小、位置、傾斜、輪廓、文本環(huán)繞方式、順序);刪除形狀DeleteShape()和讀取形狀屬性GetShapeProperties()等。調(diào)用接口方法及步驟參考以下步驟:
步驟一:dll文件獲取及導入。通過官網(wǎng)本地下載SDK文件包。(須在e-iceblue中國官網(wǎng)在線編輯板塊中注冊賬號并登錄)

創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、郊區(qū)網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、成都h5網(wǎng)站建設、電子商務商城網(wǎng)站建設、集團公司官網(wǎng)建設、外貿(mào)營銷網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為郊區(qū)等各大城市提供網(wǎng)站開發(fā)制作服務。

C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)

下載后,解壓文件,將Spire.Cloud.Word.Sdk.dll文件及其他三個dll添加引用至VS程序;或者在程序中通過Nuget搜索下載,直接導入所有dll。dll引用結果如下圖所示:
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)
步驟二:App ID及Key獲取。在“我的應用”板塊中創(chuàng)建應用以獲得App ID及App Key。
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)
步驟三:源文檔上傳。在“文檔管理”板塊,上傳源文檔。這里可以建文件夾,將文檔存放在文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用于存放源文檔及結果文檔。(云平臺提供免費1 萬次調(diào)用次數(shù)和 2G 文檔內(nèi)存)
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)


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;//源文檔密碼

            //設置形狀屬性(包括形狀類型、位置、填充顏色、旋轉方向、邊框寬度/顏色、文本環(huán)繞類型/方式
            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";//結果文檔路徑

            //調(diào)用方法添加形狀
            shapesApi.AddShape(name, paragraphPath, shapeProperties, folder, storage, indexInParagraph, password, destFilePath);
        }
    }
}

形狀添加效果:
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)

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";//結果文檔路徑

            //調(diào)用方法刪除形狀
            shapesApi.DeleteShape(name, paragraphPath, index, folder, storage, password, destFilePath);
        }
    }
}

形狀刪除效果:
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)
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();

        }
    }
}

屬性讀取結果:
C# 添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)

(本文完)

網(wǎng)頁題目:C#添加、刪除、讀取Word形狀(基于Spire.Cloud.Word.SDK)
當前網(wǎng)址:http://bm7419.com/article42/jdcihc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站維護、外貿(mào)建站網(wǎng)站收錄、微信小程序、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司