PDF水印生成

首先给PDF加上水印需要倒入两个依赖

	<dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itextpdf</artifactId>
		  <version>5.4.3</version>
	  </dependency>
	  <dependency>
		  <groupId>com.itextpdf</groupId>
		  <artifactId>itext-asian</artifactId>
		  <version>5.2.0</version>
	  </dependency>

代码:

File file = new File(需要加水印的PDF路径);
String saveFilePaths= 加上水印保存的路径;
File filetwo = new File(saveFilePaths);
try {
                if (!filetwo.exists()) {
                	//因为两个文件路径不能相同,吧原来的文件拷贝至新的文件夹下
                    copyFileUsingJava7Files(file, filetwo);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            // 要输出的pdf文件
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(file));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 将pdf文件先加水印然后输出
            try {
                setWatermark(bos, saveFilePaths,title);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(filetwo.getName());
            //生成文件之后删除拷贝文件,以免占用内存
            filetwo.delete();
        }
        if (file.exists()) {
            byte[] data;
            try {
                FileInputStream input = new FileInputStream(file);
                data = new byte[input.available()];
                int n = input.read(data);
                response.getOutputStream().write(data);
                input.close();
                System.out.println("preview pdf:" + n);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

两个实现方法:

	//文件拷贝
	private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
        java.nio.file.Files.copy(source.toPath(), dest.toPath());
    }
	//水印生成
    public static void setWatermark(BufferedOutputStream bos, String input,String title) throws Exception,
            IOException {

        PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, bos);
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);
        PdfGState gs = new PdfGState();
        for (int i = 1; i < total; i++) {
            content = stamper.getOverContent(i);// 在内容上方加水印
            //content = stamper.getUnderContent(i);//在内容下方加水印
            gs.setFillOpacity(0.2f);
            // content.setGState(gs);
            content.beginText();
            content.setColorFill(new BaseColor(148,148,148));
            content.setFontAndSize(base, 50);
            content.setTextMatrix(70, 200);
            content.showTextAligned(Element.ALIGN_CENTER, title, 300,350, 55);
            /*content.setColorFill(Color.);*/
            content.setFontAndSize(base, 8);
            content.endText();

        }
        try {
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

通过以上的方法可以实现pdf文字水印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值