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()));
    }
相关推荐
lly2024061 小时前
Bootstrap 警告框
开发语言
2601_949146531 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧1 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX2 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01032 小时前
C++课后习题训练记录Day98
开发语言·c++
爬山算法2 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7252 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎2 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄2 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
YUJIANYUE3 小时前
PHP纹路验证码
开发语言·php