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算法小屋’

猜你喜欢

转载自blog.csdn.net/weixin_43228814/article/details/130064912
pdf