springboot操作pdf(三)之pdf防伪

系列文章目录

文章列表
1. springboot操作pdf(一)之word转pdf
2. springboot操作pdf(二)之合并pdf


前言

pdf添加水印logo这种需求场景确实很少,有些时候一些文件生成pdf添加一个水印logo,做一个简单的防伪效果,虽然实际上并没有太大作用,但是产品经理说要,但是怎么去解决这个问题呢?


方案一:使用spire实现

一、导入依赖

<dependency>
	<groupId>e-iceblue</groupId>
	<artifactId>spire.office.free</artifactId>
	<version>3.1.1</version>
</dependency>

二、代码如下(示例)

        
        try {

            //创建PdfDocument实例,添加一页到PDF
            PdfDocument pdf = new PdfDocument();
            pdf.loadFromFile(pdfPath);
            PdfPageCollection page = pdf.getPages();

            //创建二维码
            BarcodeSettings settings = new BarcodeSettings();
            settings.setType(BarCodeType.QR_Code);
            settings.setData(content);
            settings.setData2D(content);
            settings.setX(1f);
            settings.setLeftMargin(0);
            settings.setShowText(false);
//        settings.setShowTextOnBottom(true);
            settings.setQRCodeECL(QRCodeECL.Q);
            settings.setQRCodeDataMode(QRCodeDataMode.Numeric);

            //生成二维码图
            BarCodeGenerator generator = new BarCodeGenerator(settings);
            BufferedImage bufferedImage = generator.generateImage();
            PdfImage pdfImage = PdfImage.fromImage(bufferedImage);


//            Rectangle2D.Float rect = new Rectangle2D.Float();
//            rect.setRect(10, 10, width, height);
            for (int i = 0; i < page.getCount(); i++) {
                PdfPageBase pageBase = page.get(i);
                pageBase.getCanvas().drawImage(pdfImage, width, height);
            }

            //保存PDF文档
            pdf.saveToFile(outPdfPath);
            pdf.dispose();


            return "成功";


        } catch (Exception e){
            e.printStackTrace();
            return "失败";
        }

方案二:使用itext实现

一、导入依赖

<dependency>
	<groupId>com.itextpdf</groupId>
	<artifactId>itextpdf</artifactId>
	<version>5.5.1</version>
</dependency>
<dependency>
	<groupId>com.itextpdf.tool</groupId>
	<artifactId>xmlworker</artifactId>
	<version>5.5.9</version>
</dependency>

二、代码如下(示例)

	/**
     * 生成二维码
     * @param contents 二维码的内容
     * @param width 二维码图片宽度
     * @param height 二维码图片高度
     */
    public static BufferedImage createQrCodeBufferdImage(String contents, int width, int height){
        Hashtable hints= new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BufferedImage image = null;
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(
                    contents,BarcodeFormat.QR_CODE, width, height, hints);
            image = toBufferedImage(bitMatrix);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return image;
    }


	/**
     * PDF附件添加二维码
     * @param output 输出文件位置
     * @param input 输入文件位置
     */
    public String setQrCodeForPDF(String output, String input, String content, int x, int y, int width, int height){
        BufferedImage bufferedImage = createQrCodeImg(content);
        try {
            //PDF附件加上二维码水印
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output));
            InputStream in = new FileInputStream(input);

            setWatermarkForPDF(out, in, bufferedImage, x, y, width, height);

            return "成功";

        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
    }

总结

两种方法均可实现给pdf添加防伪的功能,实现效果很好,推荐使用。想了解更多的使用方法,或者获取java代码都请关注公众号。

欢迎关注公众号‘CV算法小屋’

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xuxu1116

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值