java pdf 加水印

引入依赖

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

‘/static/fonts/SIMYOU.TTF’ 字体 本机没有的话, 可以百度下载

/**
     * pdf 加水印
     *
     * @return
     */
    public byte[] pdfAddWaterMark(byte[] byes) {

        String fileName = UUID.randomUUID().toString() + ".pdf";
        String courseFile = "";
        try {
            // 第二种:获取项目路径    D:\git\daotie\daotie
            //生成临时文件 , 读取完删除
            File directory = new File("");// 参数为空
            courseFile = directory.getCanonicalPath() + "/";
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] returnBytes = null;
        // 待加水印的文件
        PdfReader reader = null;
        PdfStamper stamper = null;
//        ByteArrayOutputStream baos = null;
        FileOutputStream os = null;
        try {
            reader = new PdfReader(byes);
            // 加完水印的文件
//            baos = new ByteArrayOutputStream();
//            stamper = new PdfStamper(reader, baos);
            // 加完水印的文件
            os = new FileOutputStream(courseFile + fileName);
            stamper = new PdfStamper(reader, os);

            int total = reader.getNumberOfPages() + 1;
            PdfContentByte content;
            // BaseFont font = BaseFont.createFont();
            BaseFont basefont = BaseFont.createFont("/static/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //这里的字体设置比较关键,这个设置是支持中文的写法
            /*BaseFont base = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体*/

            /*//设置透明度
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(1f);
            gs.setStrokeOpacity(1f);*/

            PdfContentByte under;
            com.itextpdf.text.Rectangle pageRect = null;

            // 循环对每页插入水印
            for (int i = 1; i < total; i++) {
                pageRect = stamper.getReader().getPageSizeWithRotation(i);
                // 计算水印X,Y坐标
                float x = (float) (pageRect.getWidth() / 1.98);
                float y = (float) (pageRect.getHeight() / 2.8);
                // 获得PDF最顶层
                under = stamper.getOverContent(i);
                under.saveState();
                // set Transparency
                PdfGState gs = new PdfGState();
                // 设置透明度为0.2
                gs.setFillOpacity(1.f);
                under.setGState(gs);
                under.restoreState();
                under.beginText();
                under.setFontAndSize(basefont, pageRect.getHeight() / 17);
                under.setColorFill(BaseColor.RED);

                // 水印文字成45度角倾斜
                System.out.println("width" + pageRect.getWidth());
                System.out.println("height" + pageRect.getHeight());
                System.out.println("x" + x);
                System.out.println("y" + y);
                under.showTextAligned(Element.ALIGN_CENTER, "图片仅供预览,不可用于商业用途", x, y, 45);
                // 添加水印文字
                under.endText();
                under.setLineWidth(1f);
                under.stroke();
            }
//            returnBytes = baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            try {
                stamper.close();
                if (os != null) {
                    os.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值