使用pdfbox 为 PDF 增加水印

使用pdfbox 为 PDF增加水印https://www.jylt.cc/#/detail?activityIndex=2&id=bd410851b0a72dad3105f9d50787f914

引入依赖

XML 复制代码
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>3.0.1</version>
</dependency>
<!--下面操作图片的时候需要用到该工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.8.25</version>
</dependency>

具体代码实现

java 复制代码
public static void main(String[] args) throws Exception {
        // 读取原始 PDF 文件
        PDDocument document = PDDocument.load(new File("/Users/a58/Downloads/test.pdf"));

        File file = new File("/Users/picture/Downloads/waterMark.png");
        BufferedOutputStream outputStream = FileUtil.getOutputStream(file);
        FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(file);

	// 创建文字图片
	Font font = new Font("宋体", Font.PLAIN, 12);
        Color fontColor = new Color(100, 100, 100, 60);
        ImgUtil.createImage("水印", font, null, fontColor, fileImageOutputStream);
	// 旋转图片,使水印倾斜
        ImgUtil.rotate(file, -20, file);

        // 遍历 PDF 中的所有页面
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            PDPage page = document.getPage(i);
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);

	    // PDF页面宽度
            float pageWidth = page.getMediaBox().getWidth();
	    // PDF页面高度
            float pageHeight = page.getMediaBox().getHeight();

	    // 4:每页有4列水印
            int xBegin = (int) (pageWidth / 4);
	    // 8:每页有8行水印
            int yBegin = (int) (pageHeight / 8);
	  
	    // 加载图片
            PDImageXObject pdImageXObject = PDImageXObject.createFromFile(file.getAbsolutePath(), document);
            float width = pdImageXObject.getWidth();
            float height = pdImageXObject.getHeight();

            for (int yIndex = 0; yIndex < 8; yIndex++) {
                for (int xIndex = 0; xIndex < 4; xIndex++) {
                    contentStream.drawImage(pdImageXObject, (xBegin * xIndex) + 5, (yBegin * yIndex), width, height);
                }
                yIndex++;
            }

            contentStream.close();
        }

        // 保存修改后的 PDF 文件
        document.save(new File("/Users/picture/Downloads/testout.pdf"));
        document.close();
        System.out.println("完成");
    }
相关推荐
像少年啦飞驰点、6 分钟前
零基础入门 Spring Boot:从“Hello World”到可部署微服务的完整学习指南
java·spring boot·微服务·编程入门·后端开发
乌蒙山连着山外山7 分钟前
idea中的工程,import有问题
java·ide·intellij-idea
1candobetter13 分钟前
JAVA后端开发——Maven 依赖传递 ≠ Spring 自动装配
java·spring·maven
生命不息战斗不止(王子晗)13 分钟前
2026面试大纲 - java数据结构与集合专题
java·数据结构·面试
若鱼191915 分钟前
SpringBoot4.0新特性-Null-safety消灭空指针
java·spring
丶小鱼丶16 分钟前
并发编程之【Java中的Thread类】
java
摇滚侠17 分钟前
IDEA Maven 项目打包标准使用指南
java·maven·intellij-idea
阿里-于怀18 分钟前
Kubernetes 官方再出公告,强调立即迁移 Ingress NGINX
java·大数据·数据库·ingress nginx
.ZGR.21 分钟前
从游戏到实战的线程进阶之旅:智能无人机防空平台
java·开发语言·无人机
JMchen12322 分钟前
Android TCP连接实战:详解一个高效可靠的TCP客户端实现
android·java·经验分享·网络协议·tcp/ip·移动开发·android-studio