zoukankan      html  css  js  c++  java
  • PDF添加二维码

    package com.xuebusi.toutiao.admin.api.common.util;
    
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Element;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.pdf.*;
    
    import java.io.*;
    
    public class WaterMarkUtils {
        /**
         * PDF附件添加二维码
         *
         * @param bos   输出文件的位置
         * @param input 输入文件流
         * @author nicky.ma
         * @date 2019年6月11日下午3:42:15
         */
        public static void setQrCodeForPDF(BufferedOutputStream bos, InputStream input) {
            try {
                // 创建水印图片
                BarcodeQRCode barcodeQRCode = new BarcodeQRCode("https://sina.cn", 100, 100, null);
                com.itextpdf.text.Image iTextImage = barcodeQRCode.getImage();
                // 水印图片位置
                iTextImage.setAbsolutePosition(10, 740);
                // 边框固定
    //            iTextImage.scaleToFit(200, 200);
                // 设置旋转弧度
                //image.setRotation(30);// 旋转 弧度
                // 设置旋转角度
    //            iTextImage.setRotationDegrees(45);
                // 设置等比缩放
                iTextImage.scalePercent(80);
                // 自定义大小
                iTextImage.scaleAbsolute(100, 100);
                //PDF附件加上二维码水印
                setWatermarkForPDF(bos, input, iTextImage);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 为PDF附件添加图片水印
         *
         * @param bos   输出文件的位置
         * @param input 输入文件流
         * @author nicky.ma
         * @date 2019/6/11 12:00:32
         */
        public static void setWatermarkForPDF(BufferedOutputStream bos, InputStream input, Image iTextImage)
                throws IOException, DocumentException {
            PdfReader reader = new PdfReader(input);
            PdfStamper stamper = new PdfStamper(reader, bos);
            int total = reader.getNumberOfPages() + 1;
            PdfContentByte waterMar;
    
            PdfGState gs = new PdfGState();
            long startTime = System.currentTimeMillis();
            System.out.println("PDF加图片水印 start");
            for (int i = 1; i < total; i++) {
    //            waterMar = stamper.getOverContent(i);// 在内容上方加水印
                waterMar = stamper.getUnderContent(1);//在内容下方加水印
                // 设置图片透明度为0.2f
                //gs.setFillOpacity(0.2f);
                // 设置笔触字体不透明度为0.4f
                //gs.setStrokeOpacity(0.4f);
                // 开始水印处理
                waterMar.beginText();
                // 设置透明度
                waterMar.setGState(gs);
                // 设置水印字体参数及大小
                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
                waterMar.setFontAndSize(bf, 12);
    //             设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
                waterMar.showTextAligned(Element.ALIGN_CENTER, "存证证书", 60, 725, 0);
                // 设置水印颜色
                waterMar.setColorFill(BaseColor.GRAY);
                // 附件加上水印图片
                waterMar.addImage(iTextImage);
                // 完成水印添加
                waterMar.endText();
                // stroke
                waterMar.stroke();
            }
            long endTime = System.currentTimeMillis();
            System.out.println("PDF加图片水印 ok 所用时间:" + (endTime - startTime) + "s");
            stamper.close();
            reader.close();
        }
    
        public static void main(String[] args) throws FileNotFoundException {
            FileInputStream inputStream = new FileInputStream(new File("D:\2021-07-29.pdf"));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\20210729.pdf"));
            WaterMarkUtils.setQrCodeForPDF(bos, inputStream);
        }
    }
  • 相关阅读:
    java网络编程【b站狂神课程笔记】
    算法设计与分析
    NP问题/NP完全问题(NP-complete problem)如何判断是否是NP完全问题
    递归的三部解题曲 关联leetcode 104. 二叉树最大深度练习
    修改typora偏好设置实现自动上传图片 关联PicGo + Gitee(码云) + typora实现markdown图床
    Typescript常见面试题
    INTEL Trusted Execution Technology (TXT) -- 基本原理
    北京大学肖臻老师《区块链技术与应用》笔记
    JavaGUI编程之贪吃蛇小游戏原码
    LeetCode 21.合并两个有序链表
  • 原文地址:https://www.cnblogs.com/jun1019/p/15316960.html
Copyright © 2011-2022 走看看