pdf添加水印

给pdf文件添加水印

  1. 引入依赖
xml 复制代码
  <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.3</version>
        </dependency>
  1. 添加水印
java 复制代码
package com.it2.pdfdemo02.util;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import java.io.FileOutputStream;

/**
 * @Description: PDF增加水印工具类
 */
public class PDFUtil {

    /**
     * 给PDF添加水印
     * @param inputFilePath 源文件
     * @param outputFilePath 生成的文件
     * @param waterMarkContent 添加水印的内容
     */
    public static void pdfAddWaterMark(String inputFilePath, String outputFilePath, String waterMarkContent) {
        try {
            // 水印的高和宽
            int waterMarkHeight = 30;
            int watermarkWeight = 60;
            // 水印间隔距离
            int waterMarkInterval = 200;
            // 读取PDF文件流
            PdfReader pdfReader = new PdfReader(inputFilePath);
            // 创建PDF文件的模板,可以对模板的内容修改,重新生成新PDF文件
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(outputFilePath));
            // 设置水印字体
            BaseFont baseFont = BaseFont.createFont("Font/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //幼圆常规
            // 设置PDF内容的Graphic State 图形状态
            PdfGState pdfGraPhicState = new PdfGState();
            // 填充透明度
            pdfGraPhicState.setFillOpacity(0.2f);
            // 轮廓不透明度
            pdfGraPhicState.setStrokeOpacity(0.4f);
            // PDF页数
            int pdfPageNum = pdfReader.getNumberOfPages() + 1;
            // PDF文件内容字节
            PdfContentByte pdfContent;
            // PDF页面矩形区域
            Rectangle pageRectangle;
            for (int i = 1; i < pdfPageNum; i++) {
                // 获取当前页面矩形区域
                pageRectangle = pdfReader.getPageSizeWithRotation(i);
                // 获取当前页内容,getOverContent表示之后会在页面内容的上方加水印
                pdfContent = pdfStamper.getOverContent(i);
                // 获取当前页内容,getOverContent表示之后会在页面内容的下方加水印
//                 pdfContent = pdfStamper.getUnderContent(i);
                pdfContent.saveState();
                // 设置水印透明度
                pdfContent.setGState(pdfGraPhicState);
                // 开启写入文本
                pdfContent.beginText();
                // 设置字体
                pdfContent.setFontAndSize(baseFont, 20);
                // 在高度和宽度维度每隔waterMarkInterval距离添加一个水印
                for (int height = waterMarkHeight; height < pageRectangle.getHeight(); height = height + waterMarkInterval) {
                    for (int width = watermarkWeight; width < pageRectangle.getWidth() + watermarkWeight;
                         width = width + waterMarkInterval) {
                        // 添加水印文字并旋转30度角
                        pdfContent.showTextAligned(Element.ALIGN_LEFT, waterMarkContent, width - watermarkWeight,
                                height - waterMarkHeight, 30);
                    }
                }
                // 停止写入文本
                pdfContent.endText();
            }
            pdfStamper.close();
            pdfReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 添加字体文件到resoures\Font\simsun.ttc

用到的字体文件(幼圆常规,C盘Windows/Fonts目录下

  1. 测试用例
java 复制代码
    @Test
    void addWater() {
        PDFUtil.pdfAddWaterMark("D:\\test3\\test1.pdf", "D:\\test3\\test1_watermark.pdf", "内部资料,禁止外传");
    }
相关推荐
南风微微吹2 小时前
2015-2025年英语四级历年真题及答案解析电子版PDF(含听力音频)
pdf·英语四级
Metaphor6926 小时前
使用 Python 将 Excel 转换为 PDF
python·pdf·excel
daanpdf6 小时前
考研英语一历年真题及答案解析PDF(2010-2026)百度网盘
考研·pdf
daanpdf8 小时前
历年考研数学一、数学二、数学三真题试卷及答案PDF
考研·pdf
南风微微吹9 小时前
2026年初级社会工作者考试历年真题及答案解析PDF电子版(2010-2025年)
pdf
俊哥工具10 小时前
不用安装不收费!多功能U盘修复工具,解决大部分U盘故障
学习·pdf·word·excel·音视频
daanpdf11 小时前
曹德旺《心若菩提》PDF及读后感
pdf
asdzx6720 小时前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
小婷资料库21 小时前
2025年新高考二卷数学真题试卷及答案解析电子版PDF
pdf·高考