前言
你好呀,我是小邹。
今天有个简单的需求:给PDF文件添加水印后保存。本文主要记录一下实现的过程,与大家共同学习。
实现
① 添加相关依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
② 工具类
package top.hqxiaozou.utils;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author:邹祥发
* @date:2022/11/24 10:19
* @description:PDF工具类,提供添加水印功能
*/
public class PDFUtils {
/**
* @param inputFile 原文件
* @param outputFile 加水印后的文件
* @param waterMarkName 水印字符
*/
public static void addWaterMark(String inputFile, String outputFile, String waterMarkName) {
PdfReader reader = null;
PdfStamper stamper = null;
try {
reader = new PdfReader(inputFile);
stamper = new PdfStamper(reader, Files.newOutputStream(Paths.get(outputFile)));
//水印字体,放到服务器上对应文件夹下
BaseFont base = BaseFont.createFont("D:/workspace/material-custom/material-custom-service/src/main/resources/arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Rectangle pageRect;
PdfGState gs = new PdfGState();
//填充不透明度 为1完全不透明
gs.setFillOpacity(0.1f);
//笔画不透明度, 为1完全不透明
gs.setStrokeOpacity(0.1f);
int total = reader.getNumberOfPages() + 1;
JLabel label = new JLabel();
FontMetrics metrics;
int textH;
int textW;
label.setText(waterMarkName);
metrics = label.getFontMetrics(label.getFont());
textH = metrics.getHeight();
textW = metrics.stringWidth(label.getText());
PdfContentByte under;
int interval = 0;
for (int i = 1; i < total; i++) {
pageRect = reader.getPageSizeWithRotation(i);
//在字体下方加水印
under = stamper.getOverContent(i);
under.beginText();
under.saveState();
under.setGState(gs);
//这里修改水印字体大小
under.setFontAndSize(base, 36);
//这里设置字体上下间隔
for (int height = interval + textH; height < pageRect.getHeight(); height = height + textH * 9) {
//这里设置字体左右间隔
for (int width = interval + textW; width < pageRect.getWidth() + textW; width = width + textW * 4) {
//这里设置倾斜角度
under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 45);
}
}
under.endText();
}
stamper.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stamper != null) {
stamper.close();
}
} catch (IOException | com.itextpdf.text.DocumentException e) {
e.printStackTrace();
}
if (reader != null) {
reader.close();
}
}
}
}
③ 测试类
package top.hqxiaozou.test;
import top.hqxiaozou.utils.PDFUtils;
/**
* @author:邹祥发
* @date:2022/11/24 10:41
* @description:PDF水印测试类
*/
public class AddWaterMarkTest {
public static void main(String[] args) {
PDFUtils.addWaterMark("C:/Users/zou/Desktop/阿里巴巴Java开发手册(终极版).pdf", "C:/Users/zou/Desktop/new.pdf", "https://www.hqxiaozou.top");
}
}

arial字体下载地址: http://d.xiazaiziti.com/en_fonts/fonts/a/Arial.ttf
arial中文不生效,可替换为: BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);