Spire.Cloud. SDK for .NET提供了接口SetBackgroudColor()、SetBackgroudImage()、DeleteBackground()、GetBackgroudColor()用於設置、刪除及讀取Word文檔背景。本文將以C#程序為例演示如何來調用API接口實現以上內容操作。
必要步驟:
步驟一:dll文件獲取及導入。在程序中通過Nuget搜索安裝,直接導入。
dll添加引用效果:

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

步驟三:源文檔上傳。在“文檔管理”板塊,上傳源文檔。這里如果想方便文檔管理,可以新建文件夾,將源文檔及結果文檔分別保存至相應的文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用於存放源文檔及結果文檔。

【示例1】設置背景顏色
using Spire.Cloud.Word; using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; using Spire.Cloud.Word.Sdk.Model; using System; namespace BackgroundColor { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號信息 Configuration wordConfiguration = new Configuration(appId, appKey); //創建BackgroundApi實例 BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration); //源文檔 var fileName = "testfile.docx"; string name = fileName; //源文檔所在文件夾,若沒有文件夾則設置為null string folder = "input"; //設置背景顏色RGB值 Color color = new Color(255, 255, 205); //設置文檔密碼,如果沒有密碼,則設置為null string password = null; //使用冰藍雲配置的2G空間存貯文檔,可設置為null string storage = null; //設置生成文檔的路徑及文檔名稱 string destFilePath = "output/BackgroundColor.docx"; //調用方法設置背景顏色 backgroundApi.SetBackgroundColor(name,color,destFilePath,folder,storage,password); } } }
背景顏色設置結果:

【示例2】設置背景圖片
using Spire.Cloud.Word.Sdk; using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; using System; namespace BackgroundImg { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號信息 Configuration wordConfiguration = new Configuration(appId, appKey); //創建BackgroundApi實例 BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration); //源文檔及圖片 var fileName = "testfile.docx"; var imageName = "ss.png"; string name = fileName; //源文檔所在文件夾,若沒有文件夾則設置為null string folder = "input"; string imagePath = "input" + "/"+ imageName; //設置文檔密碼,如果沒有密碼,則設置為null string password = null; //使用冰藍雲配置的2G空間存貯文檔,可設置為null string storage = null; //設置生成文檔的路徑及文檔名稱 string destFilePath = "output/BackgroundImg.docx"; //調用方法設置背景 backgroundApi.SetBackgroudImage(name,imagePath,folder,storage,password,destFilePath); } } }
背景圖片設置效果:

【示例3】刪除背景(包括背景顏色及背景圖片)
using Spire.Cloud.Word.Sdk; using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; using System; namespace DeleteBackground { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號信息 Configuration wordConfiguration = new Configuration(appId, appKey); //創建BackgroundApi實例 BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration); //源文檔 var fileName = "BackgroundImg.docx"; string name = fileName; //源文檔所在文件夾,若沒有文件夾則設置為null string folder = "output"; //設置文檔密碼,如果沒有密碼,則設置為null string password = null; //使用冰藍雲配置的2G空間存貯文檔,可設置為null string storage = null; //設置生成文檔的路徑及文檔名稱 string destFilePath = "output/DeleteBackground.docx"; //調用方法刪除文檔中背景 backgroundApi.DeleteBackground(name,destFilePath,password,folder,storage); } } }
文檔背景刪除效果:

【示例4】讀取背景顏色
using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; using Spire.Cloud.Word.Sdk.Model; using System; namespace GetBackground { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號信息 Configuration wordConfiguration = new Configuration(appId, appKey); //創建BackgroundApi實例 BackgroundApi backgroundApi = new BackgroundApi(wordConfiguration); //源文檔 var fileName = "BackgroundColor.docx"; string name = fileName; //源文檔密碼,若無密碼可設置為null string password = null; //源文檔所在文件夾,若沒有文件夾則設置為null string folder = "output"; //使用冰藍雲配置的2G空間存貯文檔,可設置為null string storage = null; //獲取文檔背景色 System.Console.WriteLine(backgroundApi.GetBackgroundColor(name,password,folder,storage)); System.Console.ReadLine(); } } }
背景色RGB值讀取結果:

(本文完)
