使用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("完成");
    }
相关推荐
鹏易灵8 分钟前
C++——2.常量与 const、constexpr 初识详解
java·开发语言·c++
qq_4523962311 分钟前
第十三篇:《K8s 安全基础:RBAC、ServiceAccount、Pod Security》
java·安全·kubernetes
张某布响丸辣35 分钟前
Spring AI 极简入门:Java 开发者快速上手 AI 开发
java·人工智能·spring·springai
java1234_小锋37 分钟前
请描述 Spring Boot 的启动流程,包括 SpringApplication 的初始化和 run 方法的核心步骤。
java·数据库·spring boot
疯狂成瘾者39 分钟前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表
lanyxp1 小时前
Sentinel 管不到 SQL 这一层——我写了个 MyBatis SQL 熔断器
java
武子康1 小时前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
慧一居士1 小时前
SpringCloud 微服务Feigin 用的完整调用端和被调用的示例
java·spring cloud
CodeStats1 小时前
【虚拟机】 从 CPU 指令到虚拟机隔离:虚拟机就是一个“模拟了完整硬件的普通进程”
java·docker