使用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("完成");
    }
相关推荐
程序员张34 小时前
Maven编译和打包插件
java·spring boot·maven
ybq195133454315 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339465 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8506 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT6 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue7 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
好奇的菜鸟8 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode9 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社29 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理9 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring