趣文网 > 作文大全

C 添加Word文本/图片水印(基于Spire.Cloud.Word.SDK)

2020-12-03 17:05:01
相关推荐

概述

Spire.Cloud.Word提供了watermarksApi接口可用于添加水印到Word文档,包括添加文本水印(SetTextWatermark)、图片水印(SetImageWatermark),本文将对此做详细介绍。

工具

Spire.Cloud.Word.SDK具体步骤

步骤1:dll文件获取及引用。(须在e-iceblue中文官网在线编辑板块中注册账号并登录)

可通过E-iceblue中文官网下载.NET下的SDK包,或者在Nuget网站下载获取package包,完成下载后,将Spire.Cloud.Word.Sdk.dll及其依赖项的dll添加引用至程序(如下图)

dll文件导入效果图

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

}

}

}

文本水印添加效果:

文本水印效果图

【示例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;//冰蓝云提供的2G内存

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是云端 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 文档进行操作。

阅读剩余内容
网友评论
相关内容
延伸阅读
小编推荐

大家都在看

我的什么作文300字 吸烟的危害作文 拉裤子作文 三年级动物作文300字 万能英语作文开头结尾 贴鼻子游戏作文 写人作文600字初中 春的作文300字 特别的一天作文 我的成长经历 作文 春来了作文 一件新鲜事作文 团圆作文600字 我的偶像作文400字 杜甫草堂作文 放假第一天作文 放烟火作文 光盘行动作文 南京旅游作文 老家的风景作文 包饺子作文开头 冬天的早晨作文 招聘广告英语作文 关于坚持的作文素材 劳动作文300字 关于对联的作文 家风家训家规作文 我和我的家人作文 了不起的我作文 糖醋排骨的作文