本文介紹調用Spire.Cloud.Word.SDK提供的介面shapesApi來操作Word形狀,包括添加形狀AddShape(),添加形狀時,可設置形狀類型、顏色、大小、位置、傾斜、輪廓、文本環繞方式、順序);刪除形狀DeleteShape()和讀取形狀屬性GetShapeProperties( ...
本文介紹調用Spire.Cloud.Word.SDK提供的介面shapesApi來操作Word形狀,包括添加形狀AddShape(),添加形狀時,可設置形狀類型、顏色、大小、位置、傾斜、輪廓、文本環繞方式、順序);刪除形狀DeleteShape()和讀取形狀屬性GetShapeProperties()等。調用介面方法及步驟參考以下步驟:
步驟一:dll文件獲取及導入。通過官網本地下載SDK文件包。(須在e-iceblue中國官網線上編輯板塊中註冊賬號並登錄)
下載後,解壓文件,將Spire.Cloud.Word.Sdk.dll文件及其他三個dll添加引用至VS程式;或者在程式中通過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
{
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);
}
}
}
形狀添加效果:
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";//結果文檔路徑
//調用方法刪除形狀
shapesApi.DeleteShape(name, paragraphPath, index, folder, storage, password, destFilePath);
}
}
}
形狀刪除效果:
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();
}
}
}
屬性讀取結果:
(本文完)