C# 添加Word水印(基于Spire.Cloud.SDK for .NET )
概述
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); } } }
图片水印添加效果:
(完)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2018-12-05 C# 添加Excel表单控件(Form Controls)