PDF添加水印和文字(PDF签名流程)

(1)引入jar包


com.itextpdf
itextpdf
5.2.0

(2)代码

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.lang.StringUtils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class Gyqm {
	public static void main(String[] args) {
  		Gyqm gyqm = new Gyqm();
 		 try {
            File bakFile = new File("D:/qmcs/"+"09067"+".pdf");
            if(bakFile.exists()) { 
             //文件存在 删除原文件
             File bakFile2 = new File("D:/qmcs/"+"09067"+".pdf");
             bakFile2.delete();
            }
   			gyqm.qianmingFile("20P0012","09067");//09067 05427   
  		} catch (DocumentException e) {
   			e.printStackTrace();
  		} catch (IOException e) {
   			e.printStackTrace();
  		} 
 	}
 	public void qianmingFile( String mo,String user) throws DocumentException, IOException{
	     try {
	      String mopdf="D:/qmcs/"+mo+".pdf";
	      String syqm="D:/qmcs/"+user+".png";
	      String input = FixFileName(mopdf,user);
	      System.out.println(mopdf);
	      System.out.println(input);
	      System.out.println(syqm);
	  	 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(mopdf)));
	   		Calendar cal = Calendar.getInstance();
	         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	         setWatermark(bos, input, syqm, format.format(cal.getTime()), 16);         
	  	} catch (FileNotFoundException e) {
	  			 e.printStackTrace();
	  	}      
	  }
	  /**
     * 
     * @param bos输出文件的位置
     * @param input 原PDF位置 
     * @param syqm 水印签名   
     * @param waterMarkName 页脚添加水印  审核时间
     * @param permission 权限码  
     * @throws DocumentException
     * @throws IOException
     */
     public void setWatermark(BufferedOutputStream bos, String input,String syqm, String yesTime, int permission)
            throws DocumentException, IOException {
			 PdfReader reader = new PdfReader(input);
        PdfStamper stamper = new PdfStamper(reader, bos);
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        BaseFont base = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        PdfGState gs = new PdfGState();
        String user="";
        if(StringUtils.isNotBlank(syqm)) {
         user=syqm.substring(syqm.lastIndexOf("/")+1, syqm.indexOf("."));
        }
        for (int i = 1; i < total; i++) {
			content = stamper.getOverContent(i);// 在内容上方加水印
            gs.setFillOpacity(0.2f);
            content.beginText();
            content.setColorFill(BaseColor.BLACK);
            content.setTextMatrix(70, 200);
            content.setFontAndSize(base, 11);
            Image image = Image.getInstance(syqm);
            if("09067".equals(user)) {
             image.setAbsolutePosition(1030, 45); // set the first background
                // image of the absolute
             image.scaleToFit(34, 40);
                content.showTextAligned(Element.ALIGN_CENTER, yesTime, 1120, 50, 0);
            }else {
             image.setAbsolutePosition(1030, 72); // set the first background
                // image of the absolute
             image.scaleToFit(34, 40);
                content.showTextAligned(Element.ALIGN_CENTER, yesTime, 1120, 80, 0);
            }
            content.addImage(image);
          
            content.endText();
		}
		 stamper.close();
	}
	 /**
  * *通过文件路径直接修改文件名
  * @param filePath 需要修改的文件的完整路径
  * @param newFileName 需要修改的文件的名称
  * @return
  */
  public String FixFileName(String filePath, String newFileName) {
		 File f = new File(filePath);
        if (!f.exists()) { // 判断原文件是否存在(防止文件名冲突)
            return null;
        }
        newFileName = newFileName.trim();
        if ("".equals(newFileName) || newFileName == null) // 文件名不能为空
            return null;
        String newFilePath = null;
        if (f.isDirectory()) { // 判断是否为文件夹
            newFilePath = filePath.substring(0, filePath.lastIndexOf("/")) + "/" + newFileName;
        } else {
            newFilePath = filePath.substring(0, filePath.lastIndexOf("/")) + "/" + newFileName
                    + filePath.substring(filePath.lastIndexOf("."));
        }
        File nf = new File(newFilePath);
        try {
            f.renameTo(nf); // 修改文件名
       } catch (Exception err) {
           err.printStackTrace();
           return null;
        }
        return newFilePath;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值