本文实例为大家分享了java pdf加水印的具体代码,供大家参考,具体内容如下
引入依赖
com.itextpdf.tool
xmlworker
5.5.10
com.itextpdf
itextpdf
5.5.10
‘/static/fonts/SIMYOU.TTF' 字体 本机没有的话, 可以百度下载
/**
* pdf 加水印
*
* @return
*/
public byte[] pdfAddWaterMark(byte[] byes) {
String fileName = UUID.randomUUID().toString() + ".pdf";
String courseFile = "";
try {
// 第二种:获取项目路径 D:\git\daotie\daotie
//生成临时文件 , 读取完删除
File directory = new File("");// 参数为空
courseFile = directory.getCanonicalPath() + "/";
} catch (IOException e) {
e.printStackTrace();
}
byte[] returnBytes = null;
// 待加水印的文件
PdfReader reader = null;
PdfStamper stamper = null;