PDF加水印

做一个记录,备不时之需

1.前期准备:
在pom.xml中加入以下依赖

        <!-- PDF加水印 Begin  -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <!-- PDF加水印 End  -->

2.直接贴代码

package com.cesgroup.archive.util.water;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.itextpdf.text.*;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

/**
 * @Description 上传PDF形成水印
 * @author Cc
 * @date 2020/4/15 17:12 
 */ 
public class WaterMakeUtil {
	 public static void main(String[] args) {
		 String srcFile = "D:\\AA\\000.pdf";
		 String destFile = "D:\\AA\\0002_bak.pdf";
		 Map<String,Object> cMap = new HashMap<>();
		 cMap.put("username","张三");
		 cMap.put("dwmc","公安局");
		 cMap.put("IP","127.0.0.2");
		 try {
			 addWaterMark(srcFile,destFile,cMap,10,0);
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	 }

	/**
     * 
     * 【功能描述:添加图片和文字水印】
     * @param srcFile 待加水印文件
     * @param destFile 加水印后存放地址
     * @param map 加水印的文本内容
     * @param textWidth 文字横坐标
     * @param textHeight 文字纵坐标
     * @throws Exception
	 * @date 2020/4/15 
     */
    public static void addWaterMark(String srcFile, String destFile, Map map,
            int textWidth, int textHeight) throws Exception
    {
        // 待加水印的文件
        PdfReader reader = new PdfReader(srcFile);
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        // 设置字体
        BaseFont font = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.CACHED);
        //Font font = new Font("宋体",Font.BOLD,30);
        // 循环对每页插入水印
        for (int i = 1; i < total; i++)
        {
            // 水印的起始
            //content = stamper.getUnderContent(i);
        	content = stamper.getOverContent(i);
            // 开始
            content.beginText();
            // 设置颜色 默认为蓝色
	        content.setColorFill(BaseColor.GRAY);
            // 设置字体及字号
            content.setFontAndSize(font, 18);
            // 设置起始位置
            //content.setTextMatrix(0, 300);
            //content.setTextMatrix(textWidth, textHeight);
            // 开始写入水印
            PdfGState gs=new PdfGState();
            gs.setFillOpacity(0.5f);
            content.setGState(gs);
            int position=100;
            for(int m=0;m<10;m++){
            	 content.showTextAligned(Element.ALIGN_LEFT, map.get("username").toString(), textWidth+0,position, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("dwmc").toString(), textWidth+0,position-50, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("IP").toString(), textWidth+0,textHeight+position-100,30);
                 
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("username").toString(), textWidth+200,position, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("dwmc").toString(), textWidth+200,position-50, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("IP").toString(), textWidth+200,textHeight+position-100,30);
                 
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("username").toString(), textWidth+400,position, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("dwmc").toString(), textWidth+400,position-50, 30);
                 content.showTextAligned(Element.ALIGN_LEFT, map.get("IP").toString(), textWidth+400,textHeight+position-100,30);
                 position=position+200;
                 
       
            }
            content.endText();
        }
        stamper.close();
        //注意这个也必须关闭,否则会导致源文件被jvm一直占用!!!!
        reader.close();
	    System.out.println("加水印完成:"+destFile);
    }

}

3.验证结果
在这里插入图片描述
到此完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值