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.驗證結果
在這裏插入圖片描述
到此完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章