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值讀取結果:

(本文完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/91535.html
標籤:C#
上一篇:WeihanLi.Npoi 支持 ShadowProperty 了
下一篇:CefSharp禁止彈出新表單,在同一視窗打開鏈接,或者在新Tab頁打開鏈接,并且支持帶type="POST" target="_blank"的鏈接
