Java 打印pdf添加水印实现

Java 打印pdf添加水印实现

添加水印工具类

复制代码
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.io.FileInputStream;
import java.io.FileOutputStream;

@Slf4j
public class PdfUtils {

    /**
     * pdf添加文字水印
     * @param waterMark 水印内容
     * @param sourcePath 源文件路径
     * @param targetPath 目标文件路径
     * @param fontSize 字体大小
     */
    public static String addWatermark(String waterMark, String sourcePath, String targetPath, int fontSize) throws Exception {
        if (StringUtils.isBlank(waterMark) || StringUtils.isBlank(sourcePath) || StringUtils.isBlank(targetPath)) {
            return sourcePath;
        }
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            //待加水印文件
            reader = new PdfReader(new FileInputStream(sourcePath));
            //加完水印输入的文件
            stamp = new PdfStamper(reader, new FileOutputStream(targetPath));
            // 设置字体(需要考虑pdf中文无法显示的问题),Itext内置了一些中文字体,STSongStd-Light,STSong-Light
            BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);

            //设置透明度
            PdfGState pdfGState = new PdfGState();
            pdfGState.setFillOpacity(0.5f);   //设置透明度为0.5,值的范围是0.0到1.0,其中0.0代表完全透明(即不可见),1.0代表完全不透明(即完全可见)

            // 源PDF文件的总页数
            int pageSize = reader.getNumberOfPages();

            //循环对每页添加水印
            for (int i = 1; i <= pageSize; i++) {
//            PdfContentByte under = stamp.getUnderContent(i);  //水印在之前的文本下,文本会盖住水印,如果需要文本水印一起出现,不要使用该方法
                PdfContentByte under = stamp.getOverContent(i);   //水印在之前的文本上

                //平铺水印
                for (int x = 100; x < 750; x += 200) { // 控制水印在横向上的间距
                    for (int y = 100; y < 1000; y += 300) { // 控制水印在纵向上的间距
                        under.beginText();     //开始
                        under.setFontAndSize(font, 40);    //应用字体,并将字体大小改为40
                        under.setColorFill(BaseColor.LIGHT_GRAY);     //设置字体颜色
                        under.setGState(pdfGState);  //将透明度应用到水印里
                        under.showTextAligned(Element.ALIGN_CENTER, waterMark, x, y, 45);//居中;水印内容;水印x坐标;水印y坐标;45°角
                        under.endText();
                    }
                }
            }
        }finally {
            if (stamp != null) {
                stamp.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        return targetPath;
    }
}

调用添加水印代码

复制代码
	    String waterMark = "预览";
        String sourcePath= "D:\\project\\code\\Att\\Templates\\111.pdf";
        String targetPath= "D:\\project\\code\\Att\\Templates\\222.pdf";
        String fontSize = 40;
        path = PdfUtils.addWatermark(waterMark, sourcePath, targetPath, fontSize);

打印如下:

相关推荐
骑着蜗牛撵大象32711 分钟前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
独泪了无痕14 分钟前
Hutool之LocalDateTimeUtil:让时间间隔操作如虎添翼
java
weixin_3077791344 分钟前
RAG 面试陷阱——混合检索并非无条件优越:一个条件反转实验
人工智能·python·面试·职场和发展
云和数据.ChenGuang1 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
三无少女指南2 小时前
从这里开始:Java 后端与 AI 工程的三条阅读路线
java·学习路线·ai工程
香蕉大香蕉(06敬畏市场)2 小时前
MySQL 单点详解(07)
java·数据库·mysql·ai·博客·八股
nice_lcj5202 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
骑着蜗牛撵大象3273 小时前
2026 后端 AI 工程化:Spring AI 管调用、MCP 管工具、Agent 内嵌管自治
java·人工智能·后端·spring·ai·可观测性·mcp
tryxr3 小时前
JVM 垃圾回收机制
java·jvm·gc·垃圾回收
揣一尾游鱼yu4 小时前
Spring上(IOC)笔记
java·笔记·spring·ioc