C# 添加Word水印(文本水印、图片水印)_基于Spire.Cloud.Word
admin
2023-02-15 06:20:05
0

概述
Spire.Cloud.Word提供了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.Word.SDK package,并将Spire.Cloud.Word.Sdk.dll及其依赖项的dll添加引用至程序(如下图);或者在VS程序中通过Nuget搜索安装,具体步骤可参考这里。
C# 添加Word水印(文本水印、图片水印)_基于Spire.Cloud.Word

步骤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 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);

        }
    }
}

文本水印添加效果:
C# 添加Word水印(文本水印、图片水印)_基于Spire.Cloud.Word

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

图片水印添加效果:

C# 添加Word水印(文本水印、图片水印)_基于Spire.Cloud.Word

(本文完)

相关内容

热门资讯

身高缩了,房子小了,胆子也怯了... 据日经中文网7月24日报道,日本经济长期停滞催生了“不断缩小”的社会倾向,近年来日本人的平均身高持续...
广东省内铁路即将全线停运 作者 | 第一财经 南英今年第12号台风“红霞”已于7月24日凌晨生成,预计25日夜间至26日早晨在...
中美频繁高层互动,菲律宾好自为... 7月24日,菲律宾组织7艘公务船、3艘海警舰、1艘运鱼船,并唆使大量渔船位中国黄岩岛管辖海域非法聚集...
被要求补税2250万欧元的法国... ·贝尔纳·阿尔诺。(LVMH官网)他的“帝国”离不开这个国家的形象加持,而这个国家,也早已吃定了这一...
通派龙湖御潮云上:当极核地段遇... 在2026年的郑州楼市版图中,如果要寻找一个能够同时承载“主城确定性”与“产品革新力”的坐标,通派龙...
追光引路人—记国家级线上线下混... 人物名片:吴建丽,教授,黄河科技学院医学部教师。主持国家级线上线下混合式一流课程1门、河南省线上线下...
加总理表态:若谈判破裂,会对美... 据凤凰卫视报道,7月23日,加拿大总理卡尼与各省省长和地区领导人举行会议,商讨应对美国最新一轮关税威...
从北京望中东:和平,虽远在中国... ◆笔者参与的分论坛“维护中东和平:挑战与出路”正在进行中。今年7月举行的第十四届世界和平论坛上,中东...
巨大冲击!美国AI“闭源高价售... 日本计算机社会学专家塚越健司7月24日发表题为《中国AI“Kimi K3”引发巨大冲击——撼动美国A...
人民锐评:中国籍数学家首获全球... 历史性的突破!日前,第二十一届菲尔兹奖得主名单揭晓,北大2007级本科校友王虹、邓煜双双获奖,无数网...