C# 設置、刪除、讀取Word文檔背景——基於Spire.Cloud.Word

Spire.Cloud.Word.Sdk提供了接口SetBackgroudColor()、SetBackgroudImage()、DeleteBackground()、GetBackgroudColor()用於設置、刪除及讀取Word文檔背景。本文將以C#程序爲例演示如何來調用API接口實現以上內容操做。php

必要步驟:html

步驟一:dll文件獲取及導入。經過官網下載SDK文件包。web

 

下載後,解壓文件,將Spire.Cloud.Word.Sdk.dll文件及其餘三個dll添加引用至VS程序(以下圖);或者在程序中經過Nuget搜索安裝,直接導入。api

 

 

步驟二:App ID及Key獲取。雲端建立帳號,並在「個人應用」板塊中建立應用以得到App ID及App Key。app

 

 

步驟三:源文檔上傳。在「文檔管理」板塊,上傳源文檔。這裏若是想方便文檔管理,能夠新建文件夾,將源文檔及結果文檔分別保存至相應的文件夾下。不建文件夾時,源文檔及結果文檔直接保存在根目錄。本文示例中,建了兩個文件夾,分別用於存放源文檔及結果文檔。spa

 

 

【示例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.SetBackgroudColor(name,color, folder, storage, password, destFilePath);            
        }
    }
}

背景顏色設置結果:code

 

 

【示例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);
        }
    }
}

背景圖片設置效果:htm

 

 

【示例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, password, folder, storage, destFilePath);
        }
    }
}

文檔背景刪除效果:blog

 

 

【示例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.GetBackgroudColor(name, password, folder, storage));
            System.Console.ReadLine();       
        }
    }
}

背景色RGB值讀取結果:接口

 

 

(本文完)

相關文章
相關標籤/搜索