概述
Spire.Cloud.SDK for .NET 提供了watermarksApi介面可用于添加水印,包括添加文本水印(SetTextWatermark)、圖片水印(SetImageWatermark),本文將對此做詳細介紹,
關于Spire.Cloud
Spire.Cloud是云端 Office 檔案處理軟體,支持在線創建、編輯、保存和列印 Office (Word / Excel / PPT) 檔案,支持 .NET、Java、PHP、Python、JavaScript 等多種編程語言,可操作包括DOC、DOCX、XLS、XLSX、PPT、PPTX、PDF等格式的檔案,
也可呼叫Spire.Cloud Web API SDK 提供的介面對 Word、Excel、PPT、PDF 檔案進行操作,本文以在VS程式中通過呼叫Spire.Cloud.Word.SDK來操作Word檔案為例,添加水印,
具體步驟:
步驟1:dll檔案獲取及參考,通過NuGet安裝 Spire.Cloud.Sdk for .NET 版本: 3.4.4,具體安裝步驟可參考這里,
添加參考效果如下:

步驟2:ID及Key獲取,在冰藍云網頁注冊賬號并登陸,在“我的應用”板塊創建應用程式,獲得 App ID 及 App Key,
步驟3:檔案路徑設定,在冰藍云網頁“我的檔案”板塊,分別建立input和output兩個檔案夾,并將測驗的Word檔案和圖片添加在input檔案夾下,通過VS代碼程式,生成的帶水印的Word檔案會直接保存至output檔案夾下,具體代碼操作方法,請參考以下內容,
【示例1】添加文本水印
using Spire.Cloud.Word.Sdk; using Spire.Cloud.Word.Sdk.Api; using Spire.Cloud.Word.Sdk.Client; using Spire.Cloud.Word.Sdk.Model; using System; namespace txtwatermark { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號資訊 Configuration wordConfiguration = new Configuration(appId, appKey); //創建TablesApi實體 WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration); //設定檔案夾、測驗檔案、水印字樣及水印樣式等 string folder = "input"; string storage = null; string password = null; var document = "testfile.docx"; string name = document; TextWatermark body = new TextWatermark("Watermark") { Layout = TextWatermark.LayoutEnum.Diagonal, Font = new Font(60, "宋體") { Color = new Color(100, 100, 100) } }; //呼叫SetTextWatermark介面添加水印到Word檔案 ,并保存到指定檔案路徑 string destFilePath = "output/SetTextWatermark.docx"; watermarksApi.SetTextWatermark(name,body,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 ImgWatermark { class Program { static String appId = "App ID"; static String appKey = "App Key"; static void Main(string[] args) { //配置賬號資訊 Configuration wordConfiguration = new Configuration(appId, appKey); //創建TablesApi實體 WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration); //設定檔案夾、測驗檔案、用于水印的圖片及水印樣式等 string folder = "input"; string storage = null; int scaling = 120; bool washout = true; string password = null; var document = "testfile.docx"; string name = document; string imagePath = "input/logo.png"; string destFilePath = "output/SetImageWatermark.docx"; //呼叫SetImageWatermark介面添加圖片水印到Word檔案 watermarksApi.SetImageWatermark(name,imagePath,destFilePath,folder,storage,scaling,washout,password); } } }
圖片水印添加效果:

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/96174.html
標籤:C#
上一篇:VS的Datagridview控制元件的編輯列界面例外
下一篇:C# 異步編程
