Spire.Cloud.SDK for Java提供了接口pdfWartermarkApi可用于添加文本水印addTextWartermark()和图片水印addImageWartermark()到PDF文档。文本将通过Java示例介绍具体实现方法。首先请参考以下步骤准备程序运行环境:

一、导入jar文件。(有2种方式)

(推荐)方式1. 创建Maven项目程序,通过maven仓库下载导入。以IDEA为例,新建Maven项目,在xml文件中配置maven仓库路径,并指定spire.cloud.sdk的依赖,如下:

<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> com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency> <dependency>
<groupId> com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency> <dependency>
<groupId> com.squareup.okhttp </groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency> <dependency>
<groupId> com.squareup.okio </groupId>
<artifactId>okio</artifactId>
<version>1.6.0</version>
</dependency> <dependency>
<groupId> io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency> <dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency> <dependency>
<groupId> org.threeten </groupId>
<artifactId>threetenbp</artifactId>
<version>1.3.5</version>
</dependency> </dependencies>

完成配置后,点击“Import Changes” 即可导入所有需要的jar文件。如果使用的是Eclipse,可参考这里的导入方法。

导入结果:

方式2手动下载jar包,然后解压文件,手动导入jar,同时还需另行手动导入其他几个jar文件。

二、登录冰蓝云账号,创建文件夹,上传文档。

三、创建应用程序,获取App ID及App Key

完成以上步骤后,可参考以下代码示例进行水印添加。

示例1:Java 添加PDF文本水印

import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.*;
import spire.cloud.pdf.sdk.model.*; public class TextWatermark {
//配置App ID和App Key等应用账号信息
static String appId = "App ID";
static String appKey = "App Key;
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfWartermarkApi pdfWartermarkApi = new PdfWartermarkApi(configuration); public static void main(String[] args) throws ApiException {
String name = "sample.pdf";//用于测试的PDF文档
String outPath = "output/TextWartermark.pdf";//生成的结果文档路径(这里保存在了冰蓝云的output文件夹路径下)
int pageNumber = 2;//指定需要添加文本水印的PDF页面(第二页)
String folder = "input";//测试文档所在的云端路径(将测试的PDF文件上传到冰蓝云input的文件夹下) //创建水印文本、字体、字号等
Text text = new Text();
Font font = new Font();
font.setFontType(Font.FontTypeEnum.STANDARD);
font.setFontFamily("Helvetica");
font.setFontSize(16f);
font.setFontStyle(Font.FontStyleEnum.REGULAR);
text.setFont(font);
text.setValue("This is a test."); //绘制水印,指定文本水印坐标位置及高度、宽度
RectangleF rect = new RectangleF();
rect.setX(10f);
rect.setY(10f);
rect.setWidth(100f);
rect.setHeight(50f);
text.setRect(rect); TextWartermark textWartermark = new TextWartermark();
RectangleF rec = new RectangleF();
rec.setX(10f);
rec.setY(10f);
rec.setWidth(500f);
rec.setHeight(500f);
textWartermark.setRect(rec);
textWartermark.setText(text);
textWartermark.setHorizontalCopies(2);
textWartermark.setVerticalCopies(3);
textWartermark.setTransparency(0.3f);
textWartermark.setRotationAngle(45f); //设置水印文本颜色
Color c1=new Color();
c1.setA(100);
c1.setR(0);
c1.setG(0);
c1.setB(255);
text.setBackgroundColor(c1);
Color c2=new Color();
c2.setA(100);
c2.setR(255);
c2.setG(0);
c2.setB(0);
text.setForegroundColor(c2); String storage= null;//冰蓝云提供的2G存储空间,可设置为null
String password =null;//测试文档密码,测试文档没有密码则设置为null //调用方法添加文本水印
pdfWartermarkApi.addTextWartermark(name, outPath, pageNumber, textWartermark, folder, storage, password);
}
}

文本水印添加效果:

示例2:Java添加PDF图片水印

import spire.cloud.pdf.sdk.ApiException;
import spire.cloud.pdf.sdk.Configuration;
import spire.cloud.pdf.sdk.api.PdfWartermarkApi; import java.io.File; public class ImageWatermark {
//配置App ID和App Key等应用账号信息
static String appId = "App ID";
static String appKey = "App Key";
static String baseUrl = "https://api.e-iceblue.cn";
static Configuration configuration = new Configuration(appId, appKey, baseUrl);
static PdfWartermarkApi pdfWartermarkApi = new PdfWartermarkApi(configuration); public static void main(String[] args) throws ApiException {
String name = "sample.pdf";//用于测试的PDF文档
String outPath = "output/ImageWartermark.pdf";//生成的结果文档路径(这里保存在了冰蓝云的output文件夹路径下)
int pageNumber = 2;//指定需要添加图片水印的PDF页面(第二页) File file = new File("C:\\Users\\Administrator\\Desktop\\images\\logo\\java.png");//水印图片的本地路径 //指定水印坐标位置及宽度、高度
float x = 10;
float y = 10;
float width = 500;
float height = 500; String folder = "input";//测试文档所在的云端路径(将测试的PDF文件上传到冰蓝云input的文件夹下)
String password = null;//测试文档密码,测试文档没有密码则设置为null
String storage = null;//冰蓝云提供的2G存储空间,可设置为null //调用方法添加图片水印到PDF
pdfWartermarkApi.addImageWartermark(name, outPath, pageNumber, file, x, y, width, height, folder, storage, password);
}
}

图片水印效果:

(完)

Java 在PDF中添加文本水印、图片水印(基于Spire.Cloud.SDK for Java)的更多相关文章

  1. Java 在Word中添加多行图片水印

    Word中设置水印效果时,不论是文本水印或者是图片水印都只能添加单个文字或者图片到Word页面,效果比较单一,本文通过Java代码示例介绍如何在页面中添加多行图片水印效果,即水印效果以多个图片平铺到页 ...

  2. Java 添加、删除、替换、格式化Word中的文本(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了TextRangesApi接口可通过addTextRange()添加文本.deleteTextRange()删除文本.updateTextRang ...

  3. Java 添加、下载、读取PDF附件信息(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了PdfAttachmentsApi接口添加附件addAttachment().下载附件downloadAttachment().获取附件信息get ...

  4. Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)

    Spire.Cloud.SDK for Java提供了接口PdfConvertApi通过convert()方法将PDF文档以及XPS文档转为指定文档格式,如转PDF为Word(支持Docx.Doc). ...

  5. Java 设置Excel单元格格式—基于Spire.Cloud.SDK for Java

    本文介绍使用Spire.Cloud.SDK for Java来设置Excel单元格格式,包括字体.字号.单元格背景.字体下滑线.字体加粗.字体倾斜.字体颜色.单元格对齐方式.单元格边框等 一.下载SD ...

  6. Java 设置、删除、获取Word文档背景(基于Spire.Cloud.SDK for Java)

    本文介绍使用Spire.Cloud.SDK for Java 提供的BackgroundApi接口来操作Word文档背景的方法,可设置背景,包括设置颜色背景setBackgroundColor().图 ...

  7. Java 读取PDF中的文本和图片

    本文将介绍通过Java程序来读取PDF文档中的文本和图片的方法.分别调用方法extractText()和extractImages()来读取.   使用工具:Free Spire.PDF for Ja ...

  8. Spire.Cloud.SDK for Java 合并、拆分Excel单元格

    Spire.Cloud.SDK for Java 是Spire.Cloud云产品系列中,用于处理Word.Excel.PowerPoint以及PDF文档的JAR文件,可执行文档编辑.转换.保存等操作. ...

  9. Java 在PDF 中添加超链接

    对特定元素添加超链接后,用户可以通过点击被链接的元素来激活这些链接,通常在被链接的元素下带有下划线或者以不同的颜色显示来进行区分.按照使用对象的不同,链接又可以分为:文本超链接,图像超链接,E-mai ...

  10. C# 加密、解密PDF文档(基于Spire.Cloud.SDK for .NET)

    Spire.Cloud.SDK for .NET提供了接口PdfSecurityApi可用于加密.解密PDF文档.本文将通过C#代码演示具体加密及解密方法. 使用工具: Spire.Cloud.SDK ...

随机推荐

  1. FreeRTOS 操作系统

    FreeRTOS操作系统 01 FreeRTOS 的定义和概述 定义:FreeRTOS(Free-Real-Time Operating System)是一个开源的实时操作系统内核,专门为嵌入式系统设 ...

  2. 研发三维GIS系统笔记/实现wgs84投影-002

    四叉树代码修改完善 原来的代码中,没有使用投影转换,直接使用的是世界坐标(单位是米), CELLQuadTree::CELLQuadTree( CELLTerrainInterface* pInter ...

  3. 使用 Kubernetes 简化平台工程

    平台工程在现代应用程序开发和部署中发挥的作用至关重要.随着软件应用程序变得越来越复杂和分散,对稳健且可扩展的基础设施的需求变得越来越重要.这就是平台工程的作用所在,它是支持整个软件开发生命周期的支柱. ...

  4. Python - 字典3

    修改字典项 您可以通过引用其键名来更改特定项的值: 示例,将 "year" 更改为 2018: thisdict = { "brand": "Ford ...

  5. json数组格式问题

    ---恢复内容开始--- 使用jsonserver来模拟后台数据接口时犯了一个很低级的错误 找了很久没有发现有什么不对劲的地方,后来仔细发现原来是一个很细微的语法问题:}]  中间不能有逗号!! -- ...

  6. Vue之交互

    1.get() <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. 联想think服务器centos系统安装

    一.前言 本以为安装服务器是一件轻松顺利的事,没想到一安就是两天- 二.错误列表 1.硬件系统不兼容低版本centos 刚开始安装centos6.8的操作系统总是无法找到配置好的raid阵列导致无法安 ...

  8. Tetris(俄罗斯方块).sh

    #!/bin/bash # Tetris Game # 10.21.2003 xhchen<[email]xhchen@winbond.com.tw[/email]> #APP decla ...

  9. 数据结构-线性表-双向链表(c++)

    与单循环链表类似,但析构函数需要注意 析构函数: 因为while循环的条件是p->next!=front,所以不能直接delete front: template<class T> ...

  10. .net 温故知新【13】:Asp.Net Core WebAPI 使用依赖注入DI

    一.使用DI注入 在之前的文章中已经讲过DI的概念(.net 温故知新:[7]IOC控制反转,DI依赖注入),基于控制台程序演示了DI依赖注入的使用,基于Microsoft.Extensions.De ...