Spire.Cloud.Word API给开发者提供了一个WatermarksApi类,用于添加和操作水印。本文将介绍如何使用Spire.Cloud.Word API给Word文档添加水印,包括文本水印和图片水印。
详细步骤如下:
1、首先,请在 cloud.e-iceblue.cn 网站上注册一个账户并登录,然后点击导航栏“我的应用” ,创建一个应用,获得App ID和App Key。
2、点击导航栏“文档管理”,将Word文档和水印图片上传至“我的文档”。
3、创建一个Maven应用程序,在pom.xml文件中添加Spire.Cloud.Word的Maven依赖。详细步骤参考文章 通过 Maven 仓库安装 Spire.Cloud Web API。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>cloud</name>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> cloud </groupId>
<artifactId>spire.cloud.sdk</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
4、新建Java class,调用Spire.Cloud.Word API给Word文档添加水印。
添加文本水印示例代码
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.WatermarksApi;
import spire.cloud.word.sdk.client.model.Color;
import spire.cloud.word.sdk.client.model.Font;
import spire.cloud.word.sdk.client.model.TextWatermark;
public class TextWatermark {
private static String appId = "App ID";
private static String appKey = "App Key";
public static void main(String[] args) throws ApiException {
//配置App ID和App Key
Configuration wordConfiguration = new Configuration(appId, appKey);
//创建WatermarksApi实例
WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration);
//原文档
String name = "template.docx";
//存放原文档的文件夹,没有则为null
String folder = null;
//使用冰蓝云配置的2G空间存贮文档,可设置为null
String storage = null;
//文档密码,没有则为null
String password = null;
//创建TextWatermark实例
TextWatermark txtWatermark = new TextWatermark("内部使用");
//设置水印排版方式
txtWatermark.setLayout(TextWatermark.LayoutEnum.DIAGONAL);
Color color = new Color(255, 0, 0);
Font font = new Font("宋体", 80f, color);
//设置水印字体
txtWatermark.setFont(font);
//添加文本水印到文档并保存到指定路径
String destFilePath = "output/setTextWatermark.docx";
watermarksApi.setTextWatermark(name, txtWatermark, destFilePath, folder, storage, password);
}
}
添加图片水印示例代码
import spire.cloud.word.sdk.client.ApiException;
import spire.cloud.word.sdk.client.Configuration;
import spire.cloud.word.sdk.client.api.WatermarksApi;
public class ImageWatermark {
private static String appId = "App ID";
private static String appKey = "App Key";
public static void main(String[] args) throws ApiException {
//配置App ID和App Key
Configuration wordConfiguration = new Configuration(appId, appKey);
//创建WatermarksApi实例
WatermarksApi watermarksApi = new WatermarksApi(wordConfiguration);
//原文档
String fileName = "template.docx";
//水印图片
String imagePath = "background.png";
//存放原文档的文件夹,没有则为null
String folder = null;
//使用冰蓝云配置的2G空间存贮文档,可设置为null
String storage = null;
//水印图片缩放比例
Integer scaling = 120;
//是否冲蚀
Boolean washout = true;
//文档密码,没有则为null
String password = null;
//添加图片水印到文档并保存到指定路径
String destFilePath = "output/setImageWatermark.docx";
watermarksApi.setImageWatermark(fileName, imagePath, destFilePath, folder, storage, scaling, washout, password);
}
}