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

来源:https://www.cnblogs.com/Yesi/archive/2019/12/13/12034407.html
-Advertisement-
Play Games

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


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

必要步驟:

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

 

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

 

 

步驟二: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.SetBackgroudColor(name,color, folder, storage, password, destFilePath);            
        }
    }
}

背景顏色設置結果:

 

 

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

文檔背景刪除效果:

 

 

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

 

 

(本文完)

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • EulerOS其實出來有一段時間了,一直在關註,單是僅僅也只是停留在觀望的階段,目前還沒有接入的打算;正好看到園子里的兄弟分享了華為雲免費試用的活動後,難捺激動的心情,我馬上去申請試用了一臺伺服器。 ...
  • 如果要在程式中使用DbContext,則需要先在Nuget中安裝Microsoft.EntityFrameworkCore.SqlServer ...
  • 原文:https://blogs.msdn.microsoft.com/mazhou/2017/12/12/c-7-series-part-7-ref-returns/ 背景 有兩種方法可以將一個值傳遞給一個方法: 例如,FCL(.NET Framework Class Library)中的Arra ...
  • 引用類庫 1.Install-Package Microsoft.Extensions.Caching.Memory MemoryCacheOptions 緩存配置 1.ExpirationScanFrequency 獲取或設置對過期項的連續掃描之間的最短時間間隔 2.SizeLimit 緩存是沒有 ...
  • 對於地圖坐標偏移,以leaflet為例,有如下解決辦法 方法1、修改leaflet源碼,解決地圖坐標偏移問題 方法2、將點位真實的經緯度經過偏移演算法,添加到加密的地圖上 方法3、直接對離線地圖瓦片進行糾偏 方法1需要修改源碼 方法2有缺陷,地圖依然是偏移的,如果把地圖經緯度顯示出來,經緯度也是不對的 ...
  • 9月份的時候,微軟宣佈正式發佈C 8.0,作為.NET Core 3.0發行版的一部分。C 8.0的新特性之一就是預設介面實現。在本文中,我們將一起來聊聊預設介面實現。 作者:依樂祝 原文鏈接:https://www.cnblogs.com/yilezhu/p/12034584.html 提前說下: ...
  • 前言 公司項目需要做個畫線縮放,我司稱之為瞳距縮放,簡而言之就是:2張圖,從第一張圖畫一條線,再從第二個圖畫一條線,第二條線以第一條為基準,延長到一致的長度,並同比縮放圖片;文字太枯燥,請先實例圖 例子1:以皮卡丘為例,我要把路飛的拳頭縮放到皮卡丘頭那麼大 例子2:以皮卡丘的基準,縮小路飛,與其身高 ...
  • 說明:在同一視窗打開鏈接,只要稍加改造就可以實現,這裡實現的是在新Tab頁打開鏈接,並且支持帶type="POST" target="_blank"的鏈接 github和bitbucket上相關問題: 1、WPF empty POST data when using custom popup htt ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...