Fork me on GitHub

itext向PDF添加文字图片水印

Maven坐标

<properties>
    <itextpdf.version>5.5.6</itextpdf.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>${itextpdf.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>${itextpdf.version}</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>
</dependencies>

代码

    /**
     * PDF加图片水印
     * @param pdfInPath 源文件(PDF)
     * @param imgPath 图片文件(水印)
     * @param pdfOutPath 目标输出文件(PDF)
     * @throws IOException
     * @throws DocumentException
     */
    public static void setImgWatermark(String pdfInPath,String imgPath,String pdfOutPath)
            throws  IOException, DocumentException {
        FileOutputStream fos = new FileOutputStream(pdfOutPath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        PdfReader reader = new PdfReader(pdfInPath);
        PdfStamper stamper = new PdfStamper(reader, bos);
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte waterMar;

        PdfGState gs = new PdfGState();
        for (int i = 1; i < total; i++) {
            waterMar = stamper.getOverContent(i);// 在内容上方加水印
            //waterMar = stamper.getUnderContent(1);//在内容下方加水印
            // 设置图片透明度为0.8f
            gs.setFillOpacity(0.8f);
            // 设置透明度
            waterMar.setGState(gs);
            // 创建水印图片
            Image image = Image.getInstance(imgPath);
            // 水印图片位置
            image.setAbsolutePosition(850, 80);
            // 边框固定
            image.scaleToFit(200, 200);
            // 设置旋转弧度
            //image.setRotation(30);// 旋转 弧度
            // 设置旋转角度
            //image.setRotationDegrees(45);// 旋转 角度
            // 设置等比缩放
            image.scalePercent(150);
            // 自定义大小
            image.scaleAbsolute(100,100);
            // 附件加上水印图片
            waterMar.addImage(image);
            // stroke
            waterMar.stroke();
        }
        stamper.close();
        reader.close();
    }

    /**
     * PDF添加文字水印
     * @param srcFile 源文件(PDF)
     * @param outSrcPath 目标输出文件(PDF)
     * @param watermarktext 水印(文本)
     * @throws IOException
     * @throws DocumentException
     */
    public static void addWatermark(String srcFile,String outSrcPath, String watermarktext) throws IOException, DocumentException {
        // 待加水印的文件
        PdfReader reader = new PdfReader(new FileInputStream(srcFile));

        if(outSrcPath == null || outSrcPath.trim().equals("")){
            outSrcPath = "D:\\waterMark.pdf";
        }
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outSrcPath));
        // 设置字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        // 设置透明度
        PdfGState gs = new PdfGState();
        // pdf页数
        int pageCount = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        // 循环对每页插入水印
        for (int i = 1; i < pageCount; i++)
        {
            // 水印的起始
            content = stamper.getOverContent(i);
            gs.setFillOpacity(0.5f);
            content.setGState(gs);
            // 开始
            content.beginText();
            // 设置颜色 默认为黑色
            content.setColorFill(BaseColor.LIGHT_GRAY);
            // 设置字体及字号
            content.setFontAndSize(baseFont, 50);
            // 开始写入水印
            content.showTextAligned(Element.ALIGN_BASELINE, watermarktext, 180,
                    340, 45);
            content.endText();
        }
        stamper.close();
        reader.close();
    }

调用

        /*添加图片水印*/
        String filePath1 = realPath + "\\cer\\"+CommonTool.getCurrTimeForFile()+".pdf";
        setImgWatermark(realPath + filepath,"C:\\Pictures\\qrcode.png",filePath1);

        /*添加文字水印*/
        String filePath2 = realPath + "\\cer\\"+CommonTool.getCurrTimeForFile()+".pdf";
        addWatermark(filePath1,filePath2,"这是一个水印");

效果如下
image

posted @ 2022-06-01 17:26  秋夜雨巷  阅读(1988)  评论(0编辑  收藏  举报