Java pdf添加文字和图片水印 (最完整的)

代码复制粘贴下去就可以运行 亲测

测试

public static void main(String[] args) {
        try {
		//
            PdfTest.addPngWatermark("/home/dev/bb.pdf",
                    "/home/dev/cc.pdf", "你要添加的文字");

            PdfTest.imageWatermark("/home/dev/bb.pdf",
                    "/home/dev/cc.pdf","/home/dev/3.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

添加文字水印

public static boolean addPngWatermark(String InPdfFile, String outPdfFile , String textWatermark) throws IOException, DocumentException {

        try{
            PdfReader reader = new PdfReader(InPdfFile);
            //reader.unethicalreading = true;
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                    outPdfFile));
            //这里的字体设置比较关键,这个设置是支持中文的写法
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                    BaseFont.EMBEDDED);// 使用系统字体
            int total = reader.getNumberOfPages();
            //文字的长度
            int l= textWatermark.length();
            int size=0;
            if(l<=6){
                size=22;
            }
            if(l>6 && l<=11){
                size=15;
            }
            if(l>11){
                size=10;
            }
            if(l>15){
                size=5;
            }
            Font f= new Font(base,size);
            Phrase p = new Phrase(textWatermark, f);
            // transparency
            PdfGState gs1 = new PdfGState();
            // 设置水印透明度
            gs1.setFillOpacity(0.2f);
            PdfContentByte over;
            Rectangle pagesize;
            for (int i = 1; i <=total; i++) {
                pagesize = reader.getPageSizeWithRotation(i);
                over = stamper.getOverContent(i);
                over.saveState();
                over.setGState(gs1);
                over.setTextMatrix(30, 30);
                over.setColorFill(BaseColor.GRAY);
                for (int y = 0; y < 10; y++) {
                    for (int x = 0; x < 8; x++) {
                        // 水印文字成45度角倾斜
                        ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 80 + 140 * x, 158 * y, -45);
                    }
                }
                over.restoreState();
            }
            stamper.close();
            reader.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

添加图片水印

public static boolean imageWatermark(String inputPath, String outputPath,String images) throws IOException, DocumentException {

        try{
            PdfReader reader = new PdfReader(inputPath);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPath));
            PdfGState gs1 = new PdfGState();
            gs1.setFillOpacity(0.1f);

            Image image = Image.getInstance(images);
            int n = reader.getNumberOfPages();
            PdfContentByte under;
            for (int i = 1; i <= n; i++) {
                PdfContentByte pdfContentByte = stamper.getOverContent(i);
                // 获得PDF最顶层
                under = stamper.getOverContent(i);
                pdfContentByte.setGState(gs1);

                for (int y = 0; y < 10; y++) {
                    for (int x = 0; x < 8; x++) {
                        // 水印文字成45度角倾斜
                        image.setRotation(30);// 旋转 弧度
                        // 设置旋转角度
                        image.setRotationDegrees(-45);// 旋转 角度
                        // 设置等比缩放
                        under.setColorFill(BaseColor.GRAY);
                        image.scaleToFit(80,120);
                        image.setRotation(45);
                        image.setAbsolutePosition(70 + 140 * x, 125 * y); // set the first background image of the absolute
                        pdfContentByte.addImage(image);
                    }
                }
            }
            stamper.close();
            reader.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值