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

本文介紹調用Spire.Cloud.Word.SDK提供的接口shapesApi來操做Word形狀,包括添加形狀AddShape(),添加形狀時,可設置形狀類型、顏色、大小、位置、傾斜、輪廓、文本環繞方式、順序);刪除形狀DeleteShape()和讀取形狀屬性GetShapeProperties()等。調用接口方法及步驟參考如下步驟:php

步驟一:dll文件獲取及導入。經過官網本地下載SDK文件包。(須在e-iceblue中國官網在線編輯板塊中註冊帳號並登陸)html

下載後,解壓文件,將Spire.Cloud.Word.Sdk.dll文件及其餘三個dll添加引用至VS程序;或者在程序中經過Nuget搜索下載,直接導入全部dll。dll引用結果以下圖所示:web

步驟二:App ID及Key獲取。在「個人應用」板塊中建立應用以得到App ID及App Key。api

步驟三:源文檔上傳。在「文檔管理」板塊,上傳源文檔。這裏能夠建文件夾,將文檔存放在文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用於存放源文檔及結果文檔。(雲平臺提供免費1 萬次調用次數和 2G 文檔內存)app

 

 

C# 示例代碼spa

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); } } }

形狀添加效果:code

 

2. 刪除Word中的形狀orm

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); } } }

形狀刪除效果:htm

 

3. 讀取Word形狀屬性blog

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(); } } }

 

屬性讀取結果:

 

 

(本文完)

相關文章
相關標籤/搜索