本文介紹調用Spire.Cloud.SDK for .NET 提供的接口shapesApi來操作Word形狀,包括添加形狀AddShape(),添加形狀時,可設置形狀類型、顏色、大小、位置、傾斜、輪廓、文本環繞方式、順序);刪除形狀DeleteShape()和讀取形狀屬性GetShapeProperties()等。調用接口方法及步驟參考以下步驟:
步驟一:dll文件獲取及導入。在程序中通過Nuget搜索下載,直接導入所有dll。dll引用結果如下圖所示:

步驟二:App ID及Key獲取。在“我的應用”板塊中創建應用以獲得App ID及App Key。

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

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 { //配置AppID和AppKey static string appId = "App ID"; static string appKey = "App Key"; static string basePath = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, basePath); static ShapesApi shapesApi = new ShapesApi(wordConfiguration); static void Main(string[] args) { //實例化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 shapeformat = 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,shapeformat,destFilePath,folder,storage,indexInParagraph,password); } } }
形狀添加效果:

2. 刪除Word中的形狀
using System; using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; namespace DeleteShape { class Program { //配置AppID和AppKey static string appId = "App ID"; static string appKey = "App Key"; static string basePath = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, basePath); static ShapesApi shapesApi = new ShapesApi(wordConfiguration); static void Main(string[] args) { //實例化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,destFilePath,folder,storage,password); } } }
形狀刪除效果:

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 string basePath = "https://api.e-iceblue.cn"; static Configuration wordConfiguration = new Configuration(appId, appKey, basePath); static ShapesApi shapesApi = new ShapesApi(wordConfiguration); static void Main(string[] args) { //實例化ShapesApi類 ShapesApi shapesApi = new ShapesApi(wordConfiguration); string name = "AddShape.docx";//源文檔 string paragraphPath = "Section/0/Body/0/Paragraph/0"; int index = 0;//讀取的形狀索引 string folder = "output";//源文檔所在文件夾 string storage = null;//使用冰藍雲配置的2G空間存貯文檔,可設置為null string password = null;//源文檔密碼 //讀取屬性 System.Console.WriteLine(shapesApi.GetShapeFormat(name, paragraphPath, index, folder, storage, password)); System.Console.ReadLine(); } } }
屬性讀取結果:

(完)
