欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spire.Cloud.Word 添加Word水印(文本水印、图片水印)

程序员文章站 2023-11-15 16:57:22
概述 Spire.Cloud.Word提供了watermarksApi接口可用于添加水印,包括添加文本水印(SetTextWatermark)、图片水印(SetImageWatermark),本文将对此做详细介绍。 关于Spire.Cloud Spire.Cloud是云端 Office 文档处理软件 ......

概述

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搜索安装,具体步骤可参考

Spire.Cloud.Word 添加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);

        }
    }
}

文本水印添加效果:

Spire.Cloud.Word 添加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);
        }
    }
}

图片水印添加效果:

Spire.Cloud.Word 添加Word水印(文本水印、图片水印)

 

(本文完)