多语言展示
当前在线:1481今日阅读:27今日分享:41

C# 基于Spire.Cloud.Word添加Word水印

Spire.Cloud.Word.Sdk提供了watermarksApi接口可用于添加水印,包括添加文本水印(SetTextWatermark)、图片水印(SetImageWatermark),以下经验内容将对此做详细介绍。
工具/原料
1

Spire.Cloud.Word.Sdk

2

Visual Studio

方法/步骤
1

步骤1:dll文件获取及引用。通过Nuget网站搜索下载获取Spire.Cloud.Word.Sdk package, 下载后,并将Spire.Cloud.Word.Sdk.dll及其依赖项的dll添加引用至程序(如下图):

2

步骤2:ID及Key获取。在“我的应用”板块(https://cloud.e-iceblue.cn/welcome.html)创建应用程序,获得 App ID 及 App Key。

3

步骤3:文件路径设置。在“我的文档”板块,可分别建立input和output两个文件夹,并将测试的Word文档和图片上传至input文件夹下。通过VS代码程序,生成的带水印的Word文档会直接保存至output文件夹下。如不需要建立文件夹,可直接将文档上传至根目录下。具体代码操作方法,可参考代码中文件路径设置。

【示例1】添加Word文本水印
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 inputfolder = '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, inputfolder, storage, password, destFilePath);        }    }}

2

文本水印添加效果:

【示例2】添加Word图片水印
1

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 inputfolder = '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';                        //调用SetImageWatermark接口添加图片水印到Word文档 ,并保存到指定文件路径            string destFilePath = 'output/SetImageWatermark.docx';            watermarksApi.SetImageWatermark(name, imagePath, inputfolder, storage, scaling, washout, password, destFilePath);        }    }}

2

图片水印添加效果:

推荐信息