当前位置: 首页 > article >正文

itextpdf使用:使用PdfReader添加图片水印

gitee参考代码地址:https://gitee.com/wangtianwen1996/cento-practice/tree/master/src/test/java/com/xiaobai/itextpdf
参考文章:https://www.cnblogs.com/wuxu/p/17371780.html

1、生成带有文字的图片

使用java.awt包的相关类生成带文字的图片,代码如下:

/**
     * 生成带文字的图片
     * @return
     */
    public static String createImage() {
        int imageWidth = 80;
        int imageHeight = 30;
        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        Color c = new Color(255, 255, 255);
        g2.setColor(c);// 设置背景色
        g2.fillRect(0, 0, imageWidth, imageHeight);

        String code = "000153";
        // 设置文字字体
        Font font = new Font(null, Font.PLAIN, 10);
        g2.setFont(font);
        g2.setColor(new Color(0, 0, 0));
        // 文字起始位置
        g2.drawString(code, 5, 15);
        g2.dispose();

        String imagePath = "D:\\usr\\local\\zeus\\resource\\temp/aaa.jpg";
        OutputStream baos = null;
        try {
            baos = new FileOutputStream(new File(imagePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            ImageIO.write(image, "jpg", baos);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭输出流
        IOUtils.closeQuietly(baos);

        return imagePath;
    }

2、使用itextpdf的PdfReader插入图片水印

@Test
public void addImage() {
    String pdfPath = "/Users/outenmon/Public/工作资料/公告/aaaa.pdf";
    PdfReader reader = null;
    try {
        reader = new PdfReader(pdfPath, "PDF".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    String outPdfFile = "/Users/outenmon/Public/工作资料/公告/bbbb.pdf";
    PdfStamper stamp = null;
    try {
        stamp = new PdfStamper(reader, new FileOutputStream(new File(outPdfFile)));
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    PdfContentByte under;

//        PdfGState gs1 = new PdfGState();
//        gs1.setFillOpacity(0.3f);// 透明度设置

    String imagePath = ImageUtil.createImage();
    Image img = null;// 插入图片水印
    try {
        img = Image.getInstance(imagePath);
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Rectangle pageSize1 = reader.getPageSize(1);
    float height = pageSize1.getHeight();
    img.setAbsolutePosition(10, height - 50); // 坐标
    // img.setRotation(-20);// 旋转 弧度
    // img.setRotationDegrees(45);// 旋转 角度
//        img.scaleAbsolute(80, 30);// 自定义大小
    // img.scalePercent(50);//依照比例缩放

//        int pageSize = reader.getNumberOfPages();// 原pdf文件的总页数
    /*for (int i = 1; i <= pageSize; i++) {
        under = stamp.getUnderContent(i);// 水印在之前文本下
        // under = stamp.getOverContent(i);//水印在之前文本上
        under.setGState(gs1);// 图片水印 透明度
        try {
            under.addImage(img);// 图片水印
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }*/
    under = stamp.getUnderContent(1);// 水印在之前文本下
    // under = stamp.getOverContent(i);//水印在之前文本上
//        under.setGState(gs1);// 图片水印 透明度
    try {
        under.addImage(img);// 图片水印
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    try {
        stamp.close();// 关闭
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

http://www.kler.cn/news/232739.html

相关文章:

  • 【Unity】重力场中的路径预测方法
  • 排序算法---插入排序
  • 在django中集成markdown文本框
  • Unity类银河恶魔城学习记录5-1.5-2 P62-63 Creating Player Manager and Skill Manager源代码
  • golang 通过 cgo 调用 C++ 库
  • 2024.1.30力扣每日一题——使循环数组所有元素相等的最少秒数
  • 【MySQL进阶之路】SpringBoot 底层如何去和 MySQL 交互了呢?
  • 浏览器提示ERR_SSL_KEY_USAGE_INCOMPATIBLE解决
  • Node.js JSON Schema Ajv依赖库逐步介绍验证类型和中文错误提示
  • elementui上传文件不允许重名
  • Java中 使用Lambda表达式实现模式匹配和类型检查
  • 云服务器也能挂游戏 安卓模拟器
  • 树莓派-Ubuntu22.04
  • 【Unity游戏设计】跳一跳Day1
  • 深度学习预备知识1——数据操作
  • 设置了.gitignore文件,但某些需要被忽略的文件仍然显示
  • Git介绍和常用命令说明
  • 微软.NET6开发的C#特性——委托和事件
  • SpringMVC-组件解析
  • vscode 括号 python函数括号补全
  • 【Flink】FlinkSQL的DataGen连接器(测试利器)
  • arkTS开发鸿蒙OS应用(登录页面实现,连接数据库)
  • 158基于matlab的用于分析弧齿锥齿轮啮合轨迹的程序
  • flink反压及解决思路和实操
  • (十八)springboot实战——spring securtity注解方式的授权流程源码解析
  • 如何连接ChatGPT?无需科学上网,使用官方GPT教程
  • AT_abl_d 题解
  • Java基础常见面试题总结-并发(二)
  • 淘宝镜像到期如何切换镜像及如何安装淘宝镜像
  • Git版本与分支