使用 iText 库给PDF文件加水印

iText 库介绍

iText 是一款流行的 Java PDF 库,它可以用来创建、读取、修改和提取 PDF 内容。iText 提供了许多 API,包括添加文本水印的功能。

注释:创建一个Springboot来测试 iText的使用,创建项目不做过多介绍

需要的依赖

       <!-- iText 是一款流行的 Java PDF-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>

        <!-- hutool  -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.8</version>
        </dependency>

水印样式封装

创建 ITextAttributes 用于封装pdf水印样式

package com.test.testdemo.itext;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import lombok.Data;

import java.io.IOException;

/**
 * @Author Chenry.r
 * @Date 9/9/2023 下午10:11
 * @Version 1.0
 * @Description <p>备注:封装pdf水印样式</p>
 */
@Data
public class ITextAttributes {

    //字体样式
    private BaseFont fontStyle = BaseFont.createFont();
    //字体大小
    private float fontSize = 36f;
    //字体颜色
    private BaseColor fontColor = BaseColor.LIGHT_GRAY;
    //水印位置
    private int location = Element.ALIGN_CENTER;
    //水印内容
    private String showTxt = "Chenry.r制作,欢迎查看";
    //坐标以及旋转角度
    private float x = 300, y = 400, rotation = 45;


    public ITextAttributes() throws DocumentException, IOException {
    }

    public ITextAttributes(BaseFont fontStyle, float fontSize, BaseColor fontColor, int location, String showTxt, float x, float y, float rotation) throws DocumentException, IOException {
        this.fontStyle = fontStyle;
        this.fontSize = fontSize;
        this.fontColor = fontColor;
        this.location = location;
        this.showTxt = showTxt;
        this.x = x;
        this.y = y;
        this.rotation = rotation;
    }
}

封装工具类

创建 ItextWatermarkUtil 封装水印添加工具类
注意:这里你添加的水印如果有汉字的话可能要下载一个simsun.ttf字体放到 resources\fonts\simsun.ttf目录下
下载:simsun.ttf(微软宋体)
链接: https://pan.baidu.com/s/1-a7rZp6NIO0jaFr4adIWEw 提取码: 45xd

package com.test.testdemo.util;

import cn.hutool.core.io.IoUtil;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;
import com.test.testdemo.itext.ITextAttributes;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * @Author Chenry.r
 * @Date 9/9/2023 下午10:16
 * @Version 1.0
 * @Description <p>备注:pdf添加水印工具类</p>
 */
public class ItextWatermarkUtil {

    /**
     * @param pdfPath:    要处理的pdf路径
     * @param outPath:    输出pdf文件路径
     * @param attributes:
     * @Des: 这个方法处理不了中文水印, 可能因为我的
     * @Author: Chenry.r
     * @Date: 9/9/2023 下午11:07
     */
    public static void addWaterMark1(String pdfPath, String outPath, ITextAttributes attributes) {
        try {
            System.out.println("--------------开始添加水印--------------------");
            // 读取原始 PDF 文件
            PdfReader reader = new PdfReader(pdfPath);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outPath));
            // 获取 PDF 中的页数
            int pageCount = reader.getNumberOfPages();
            // 添加水印
            for (int i = 1; i <= pageCount; i++) {
                PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
                contentByte.beginText();
                contentByte.setFontAndSize(attributes.getFontStyle(), attributes.getFontSize());
                contentByte.setColorFill(attributes.getFontColor());
                contentByte.showTextAligned(attributes.getLocation(), attributes.getShowTxt(), attributes.getX(), attributes.getY(), attributes.getRotation());
                contentByte.endText();
                System.out.println("--------------已完成第" + (i) + "页的水印添加--------------------");
            }
            // 保存修改后的 PDF 文件并关闭文件流
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("--------------添加水印完成--------------------");
        }

    }

    /**
     * @param pdfPath:    要处理的pdf路径
     * @param outPath:    输出pdf文件路径
     * @param attributes:
     * @Des: 处理带汉字的水印
     * @Author: Chenry.r
     * @Date: 9/9/2023 下午11:06
     */
    public static void addWaterMark2(String pdfPath, String outPath, ITextAttributes attributes) {
        try {
            System.out.println("--------------开始添加水印--------------------");
            // 读取原始 PDF 文件
            PdfReader reader = new PdfReader(pdfPath);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outPath));
            // 获取 PDF 中的页数
            int pageCount = reader.getNumberOfPages();
            // 添加水印
            PdfContentByte content = null;
            byte[] fontBytes = IoUtil.readBytes(new ClassPathResource("fonts/simsun.ttf").getInputStream());
            for (int i = 1; i <= pageCount; i++) {
                content = stamper.getOverContent(i);
                content.beginText();
                content.setFontAndSize(BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true, fontBytes, null), attributes.getFontSize());
                content.setColorFill(attributes.getFontColor());
                content.showTextAligned(attributes.getLocation(), attributes.getShowTxt(), attributes.getX(), attributes.getY(), attributes.getRotation());
                content.endText();
                System.out.println("--------------已完成第" + (i) + "页的水印添加--------------------");
            }
            // 保存修改后的 PDF 文件并关闭文件流
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("--------------添加水印完成--------------------");
        }

    }

    /**
     * @param inputStream:  PDF输入流
     * @param outputStream: PDF输出流
     * @param watermark:    水印内容
     * @Des: 使用流的形式给PDF文件添加水印
     * @Author: Chenry.r
     * @Date: 9/9/2023 下午11:17
     */
    private static void addPdfWatermark(InputStream inputStream, OutputStream outputStream, String watermark) {
        // 使用iText向PDF文件添加水印
        try {
            System.out.println("--------------开始添加水印--------------------");
            PdfReader pdfReader = new PdfReader(inputStream);
            PdfStamper stamper = new PdfStamper(pdfReader, outputStream);
            int total = pdfReader.getNumberOfPages();
            PdfContentByte content = null;
            byte[] fontBytes = IoUtil.readBytes(new ClassPathResource("fonts/simsun.ttf").getInputStream());
            PdfGState gs = new PdfGState();
            for (int i = 1; i <= total; i++) {
                content = stamper.getOverContent(i);
                gs.setFillOpacity(0.3f);
                content.setGState(gs);
                content.beginText();
                content.setColorFill(BaseColor.RED);
                content.setFontAndSize(BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true, fontBytes, null), 30F);
                content.showTextAligned(Element.ALIGN_CENTER, watermark, 300F, 400F, 45F);
                content.endText();
            }
            stamper.close();
            pdfReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("--------------添加水印完成--------------------");
        }
    }

    public static void main(String[] args) throws IOException, DocumentException {

        //  测试addPdfWatermark方法
        InputStream inputStream = new FileInputStream("D:\\Users\\ThisPc\\桌面\\我的文件\\量身定制版-Java基础面试题.pdf");
        OutputStream outputStream = new FileOutputStream("D:\\Users\\ThisPc\\桌面\\新建文件夹\\量身定制版-Java基础面试题.pdf");
        String watermark = "Chenry.r制作,欢迎查看2023年9月9日";
        addPdfWatermark(inputStream, outputStream, watermark);
        //  测试addWaterMark、addWaterMark2方法
        //addWaterMark2("D:\\Users\\ThisPc\\桌面\\我的文件\\量身定制版-Java基础面试题.pdf", "D:\\Users\\ThisPc\\桌面\\新建文件夹\\量身定制版-Java基础面试题.pdf", new ITextAttributes());
    }
}

内容笔记

1、在添加水印之前,需要读取原始 PDF 文件:

PdfReader reader = new PdfReader("original.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));

2、然后,遍历 PDF 中的所有页面,并使用 PdfContentByte 添加水印:

// 获取 PDF 中的页数
int pageCount = reader.getNumberOfPages();

// 添加水印
for (int i = 1; i <= pageCount; i++) {
    PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
    contentByte.beginText();
    contentByte.setFontAndSize(BaseFont.createFont(), 36f);
    contentByte.setColorFill(BaseColor.LIGHT_GRAY);
    contentByte.showTextAligned(Element.ALIGN_CENTER, "Watermark", 300, 400, 45);
    contentByte.endText();
}

3、最后,需要保存修改后的 PDF 文件并关闭文件流:

stamper.close();
reader.close();

4、示例:

package com.test.testdemo.test;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author Chenry.r
 * @Date 10/9/2023 上午12:47
 * @Version 1.0
 * @Description <p>备注:示例</p>
 */
public class ItextWatermark {

    public static void main(String[] args) throws IOException, DocumentException {
        // 读取原始 PDF 文件
        PdfReader reader = new PdfReader("D:\\Users\\ThisPc\\桌面\\我的文件\\量身定制版-Java基础面试题.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("D:\\Users\\ThisPc\\桌面\\新建文件夹\\量身定制版-Java基础面试题-水印.pdf"));

        // 获取 PDF 中的页数
        int pageCount = reader.getNumberOfPages();

        // 添加水印
        for (int i = 1; i <= pageCount; i++) {
            PdfContentByte contentByte = stamper.getUnderContent(i); // 或者 getOverContent()
            contentByte.beginText();
            contentByte.setFontAndSize(BaseFont.createFont(), 36f);
            contentByte.setColorFill(BaseColor.LIGHT_GRAY);
            contentByte.showTextAligned(Element.ALIGN_CENTER, "Watermark", 300, 400, 45);
            contentByte.endText();
        }

        // 保存修改后的 PDF 文件并关闭文件流
        stamper.close();
        reader.close();
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用 Java 语言实现给 PDF 文件水印的代码: ```java import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfPage; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.property.TextAlignment; import java.io.File; import java.io.IOException; public class PdfWatermark { public static void main(String[] args) throws IOException { String srcFile = "document.pdf"; // 原始 PDF 文件 String destFile = "document_with_watermark.pdf"; // 水印后的 PDF 文件 String watermark = "Confidential"; // 要添水印文本 addWatermark(srcFile, destFile, watermark); // 调用添水印的方法 } public static void addWatermark(String srcFile, String destFile, String watermark) throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(destFile)); Document doc = new Document(pdfDoc); for (int pageNum = 1; pageNum <= pdfDoc.getNumberOfPages(); pageNum++) { PdfPage page = pdfDoc.getPage(pageNum); Rectangle pageSize = page.getPageSize(); PdfCanvas canvas = new PdfCanvas(page); canvas.beginText(); canvas.setFontAndSize(null, 50); canvas.setColor(Color.GRAY, true); canvas.setTextMatrix(pageSize.getWidth() / 2, pageSize.getHeight() / 2); canvas.showTextAligned(new Paragraph(watermark).setBold().setFontColor(Color.GRAY).setTextAlignment(TextAlignment.CENTER), pageSize.getWidth() / 2, pageSize.getHeight() / 2, pageNum, TextAlignment.CENTER, com.itextpdf.kernel.geom.VerticalAlignment.MIDDLE, 45); canvas.endText(); } doc.close(); System.out.println("水印成功!"); } } ``` 以上代码中,我们使用iText 来操作 PDF 文件。在添水印时,我们首先打开原始 PDF 文件,并创建一个 Document 对象。然后,我们遍历 PDF 文件中的所有页面,为每个页面添水印。为了添水印,我们使用PdfCanvas 类,它提供了一组方法来绘制文本、图形等。在我们的例子中,我们使用 beginText() 方法开始绘制文本,setFontAndSize() 方法设置字体和字号,setColor() 方法设置颜色,setTextMatrix() 方法设置文本的位置,showTextAligned() 方法绘制文本,并使用 endText() 方法结束绘制。最后,我们使用 Document 对象的 close() 方法关闭 PDF 文件,并输出一条水印成功的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值