本文介紹呼叫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(); } } }
屬性讀取結果:

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/77398.html
標籤:C#
下一篇:C#面向物件--類
