Java 实现PDF添加水印

maven依赖:

xml 复制代码
   <dependency>
     <groupId>com.itextpdf</groupId>
       <artifactId>itextpdf</artifactId>
       <version>5.4.3</version> 
   </dependency>

网络地址添加水印代码:

java 复制代码
    public static boolean waterMarkNet(String inputFile, String outputFile, String waterMarkName) {
        try
        {
            URL url = new URL(inputFile);
            PdfReader reader = new PdfReader(url);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            //这里的字体设置比较关键,这个设置是支持中文的写法
            BaseFont base = BaseFont.createFont();
            //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);// 使用系统字体

            int total = reader.getNumberOfPages() + 1;
            PdfContentByte under;
            Rectangle pageRect = null;
            PdfGState gs = new PdfGState();
            //透明度
            gs.setFillOpacity(0.2f);
            gs.setStrokeOpacity(0.1f);
            for (int i = 1; i < total; i++)
            {
                pageRect = stamper.getReader().
                        getPageSizeWithRotation(i);
                under = stamper.getOverContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 50);
                under.setTextMatrix(30, 30);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 100, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 300, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 500, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 700, 20);
                // 添加水印文字
                under.endText();
                under.setLineWidth(10f);
                under.stroke();
            }
            stamper.close();
            return true;
        } catch (Exception e)
        {
            e.printStackTrace();
            return false;
       

本地地址添加水印代码

java 复制代码
/**
     * 功能描述: 本地文件添加水印
     *
     * @param inputFile
     * @param outputFile
     * @param waterMarkName
     * @date 10:52 2024/11/20
     */
    public static boolean waterMark(String inputFile, String outputFile, String waterMarkName) {
        try
        {
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            //这里的字体设置比较关键,这个设置是支持中文的写法
            BaseFont base = BaseFont.createFont();
            //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   BaseFont.EMBEDDED);// 使用系统字体
            int total = reader.getNumberOfPages() + 1;
            PdfContentByte under;
            Rectangle pageRect = null;
            PdfGState gs = new PdfGState();
            //透明度
            gs.setFillOpacity(0.2f);
            gs.setStrokeOpacity(0.1f);
            for (int i = 1; i < total; i++)
            {
                pageRect = stamper.getReader().
                        getPageSizeWithRotation(i);
                under = stamper.getOverContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 50);
                under.setTextMatrix(30, 30);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 100, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 300, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 500, 20);
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 700, 20);
                // 添加水印文字
                under.endText();
                under.setLineWidth(10f);
                under.stroke();
            }
            stamper.close();
            return true;
        } catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

测试代码

java 复制代码
  public static void main(String[] args) {
       System.out.println(TestwaterMark.waterMark("https://XXXXXX/%E6%AD%A3%E6%96%87.pdf", "D:\\XXX\\XXX.pdf", "zymz"+DateUtils.dateTimeNow()));
    }
相关推荐
Java面试题总结9 分钟前
Python 开发技巧 · 高级装饰器 —— 从基础到工业级实战
开发语言·python
一路向北North23 分钟前
Spring Security OAuth2.0(19):JWT令牌
java·后端·spring
玛卡巴卡ldf27 分钟前
【LeetCode 手撕算法】(细节知识点总结)
java·数据结构·算法·leetcode·力扣
一叶飘零_sweeeet1 小时前
IDEA 插件 Trae AI 最新全攻略(基于 TRAE AI: Coding Assistant 1.7.0.0)
java·intellij-idea·trae
liulilittle1 小时前
Exhaustive drift-fix simulation V2 — KCC on shared-bottleneck wired path.
开发语言·网络·python·tcp/ip·计算机网络·信息与通信·通信
KobeSacre1 小时前
CyclicBarrier 源码
java·jvm·算法
MacroZheng1 小时前
给Claude Code装上这款炫酷的HUD插件,状态信息就一目了然了!
java·人工智能·后端
我登哥MVP2 小时前
Hadoop成长史-从Nutch子项目到大数据生态王者
java·大数据·hadoop·分布式·云原生·云计算
进击切图仔2 小时前
SAM3 微调标注流水线和 Label Studio
java·服务器·前端
名字还没想好☜2 小时前
Go error 处理:errors.Is/As 与错误包装
开发语言·后端·golang·go·错误处理